您的位置:首页 > 产品设计 > UI/UE

tableView的一些基本方法

2015-07-30 13:42 471 查看
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;
}


这个方法是点击单元格时会在单元格上打上钩。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息