tableView实现UITableViewDataSource和UITableViewDaraDelegate协议。
UITableViewDataSource中的
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
这个方法把一个cell添加到indexPath这个位置上去。indexPath中包含了第几个section indexPath.section和在这个section中第几个 cell,indexPath.row。
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [cellArr count]
}
方法返回一个整形,内容是每个section内的Cell数目,section的默认值是1。
————以上这两个方法必须实现.
下面写的是设置section的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [cellArr count];
}
分完组以后不把每个section设置一个title看上和一个section一样,设置每个Section标题的方法是
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *title = [NSString stringWithFormat:@"第%ld个section",(long)section + 1];
return title;
}
cell的点击事件:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
lastPath = indexPath;
}
这个方法是点击单元格时会在单元格上打上钩。