您好!对于学习UITableView。 下列我们通过一些代码来渐渐拓展我们的视野范围吧!!
最后我已经经付上github源代码了!!
从界面可以看出上面是一个UINavigationBar和下面是一个UITabBar。至于这两部分,我会在后来把这小项目写上去。现在我们来了解下UITableView 的使用。
这是一个分组的列表 UITableViewStyleGroup 。
typedef NS_ENUM(NSInteger, UITableViewStyle) {
UITableViewStylePlain, // regular table view
UITableViewStyleGrouped // preferences style table view
};
我们需要对每一行的UITableViewCell进行布置。为了以后代码的维护行,我们需要对cell进行一定的封装。方便今后好维护代码。
首先我们需要创建两个简单的模型并继承NSObject ,一个是JHCommonGroup和JHCommonItem分别代表的所有对应的组和对应的行
里面拥有着自己的特有属性
JHCommonItem类:
@interface JHCommonItem : NSObject
/** 图标*/
@property(nonatomic,copy) NSString *icon;
/** 标题*/
@property(nonatomic,copy) NSString *title;
/** 子标题*/
@property(nonatomic,copy) NSString *subtitle;
+(instancetype)itemWithTitle:(NSString *)title icon:(NSString *)icon;
+(instancetype)itemWithTitle:(NSString *)title;
.m文件:
@implementation JHCommonItem
+(instancetype)itemWithTitle:(NSString *)title icon:(NSString *)icon
{
JHCommonItem *item = [[JHCommonItem alloc] init];
item.title = title;
item.icon = icon;
return item;
}
+(instancetype)itemWithTitle:(NSString *)title
{
return [self itemWithTitle:title icon:nil];
}
JHCommonGroup类:
@interface JHCommonGroup : NSObject
/** 组头*/
@property (nonatomic,copy) NSString *header;
/** 组尾*/
@property (nonatomic,copy) NSString *footer;
/** 这组的所有行模型(数组中存放的都是JHCommonItem模型) */
@property (nonatomic,strong) NSArray *items;
+(instancetype)group;
另外我们还要创建一个JHCommonCell并继承UITableViewCell,如果需要在cell里面加入一些特殊的图标、文字需要调整其范围 -(void)layoutSubviews;
JHCommonCell类:
#import "JHCommonCell.h"
#include "JHCommonGroup.h"
#include "JHCommonItem.h"
@implementation JHCommonCell
+(instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString *ID = @"cell";
JHCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[JHCommonCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
}
return cell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// 设置标题的字体
self.textLabel.font = [UIFont boldSystemFontOfSize:15];
self.detailTextLabel.font = [UIFont systemFontOfSize:12];
}
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
self.detailTextLabel.x = CGRectGetMaxX(self.textLabel.frame) +3;
}
-(void)setItem:(JHCommonItem *)item
{
self.imageView.image = [UIImage imageNamed:item.icon];
self.textLabel.text = item.title;
self.detailTextLabel.text = item.subtitle;
}
@end
一些模型类定义好后,我们的工作还是需要放在控制器身上,由于我们定义了一个JHDiscoverViewController的控制器并继承至UITableViewController.所以内部已经帮我们设置好的代理和数据源方法(UITableViewDelegate,UITableViewDataSource), 还有还要了解@required和@optional的含义。
#import "JHDiscoverViewController.h"
#import "JHSearchBar.h"
#import "JHCommonCell.h"
#import "JHCommonItem.h"
#import "JHCommonGroup.h"
@interface JHDiscoverViewController ()
@property (nonatomic,strong) NSMutableArray *groups;
@end
@implementation JHDiscoverViewController
-(NSMutableArray *)groups
{
if (_groups == nil) {
self.groups = [NSMutableArray array];
}
return _groups;
}
- (id)init
{
return [self initWithStyle:UITableViewStyleGrouped];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.backgroundColor = JHColor(211, 211, 211);
// 创建搜索框对象
JHSearchBar *searchBar = [JHSearchBar searchBar];
searchBar.width = 300;
searchBar.height = 30;
self.navigationItem.titleView = searchBar;
// 初始化数据
[self setupgroup];
}
-(void)setupgroup
{
[self group0];
[self group1];
[self group2];
}
-(void)group0
{
JHCommonGroup *group = [JHCommonGroup group];
[self.groups addObject:group];
JHCommonItem *hotStatus = [JHCommonItem itemWithTitle:@"热门微博" icon:@"hot_status"];
hotStatus.subtitle = @"笑话,娱乐,神最右都搬到这啦";
JHCommonItem *findPeople = [JHCommonItem itemWithTitle:@"找人" icon:@"find_people"];
findPeople.subtitle = @"名人、有意思的人尽在这里";
group.items = @[hotStatus, findPeople];
}
-(void)group1
{
JHCommonGroup *group = [JHCommonGroup group];
[self.groups addObject:group];
// 2.设置组的所有行数据
JHCommonItem *gameCenter = [JHCommonItem itemWithTitle:@"游戏中心" icon:@"game_center"];
JHCommonItem *near = [JHCommonItem itemWithTitle:@"周边" icon:@"near"];
JHCommonItem *app = [JHCommonItem itemWithTitle:@"应用" icon:@"app"];
group.items = @[gameCenter, near, app];
}
-(void)group2
{
JHCommonGroup *group = [JHCommonGroup group];
[self.groups addObject:group];
// 2.设置组的所有行数据
JHCommonItem *video = [JHCommonItem itemWithTitle:@"视频" icon:@"video"];
JHCommonItem *music = [JHCommonItem itemWithTitle:@"音乐" icon:@"music"];
JHCommonItem *movie = [JHCommonItem itemWithTitle:@"电影" icon:@"movie"];
JHCommonItem *cast = [JHCommonItem itemWithTitle:@"播客" icon:@"cast"];
cast.subtitle = @"(10)";
JHCommonItem *more = [JHCommonItem itemWithTitle:@"更多" icon:@"more"];
group.items = @[video, music, movie, cast, more];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.groups.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
JHCommonGroup *group = self.groups[section];
return group.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
JHCommonCell *cell = [JHCommonCell cellWithTableView:tableView];
JHCommonGroup *group = self.groups[indexPath.section];
//传递模型数据
cell.item = group.items[indexPath.row];
return cell;
}
这只是一个简单的界面,还可以再进行一个维护,如果您有什么自己的想法,请告诉我,再次感谢 !!
源码下载地址:https://github.com/ios-cjh/JHSinaWeibo
QQ: 584837022