UITableView基本使用
- 遵守
UITableViewDataSource
和UITableViewDelegate
协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
return cell;
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] init];
_tableView.frame = self.view.frame;
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.rowHeight = 100;
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
}
return _tableView;
}
UITableView常见属性
self.tableView.separatorColor = [UIColor redColor];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.tableView setseparatorInset:UIEdgeInsetsMake(0,0,0,0)];
tableView.allowsSelection = NO;
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:10 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES]
- 实现右侧的索引栏,,点击右侧索引栏中的文字,会根据索引的顾序跳转到左侧对应的位置,获取
groups
数组中的每个对象的title
值,并返回到一个NSArray
中
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return [self.groups valueForKeyPath:@"title"];
}
UITableViewCell的常见属性
自定义UITableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentfier {}
Xib在中UITableViewCell的复用
[tableView registerNib:[UINib nibWithNibName:@"JJTableCell" bundle:nil] forCellReuseIdentifier:@"JJTableCell"];
JJTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JJTableCell"];