iPhone+;UITableView+;访问特定行的单元格
我在申请中使用以下方法:iPhone+;UITableView+;访问特定行的单元格,iphone,uitableview,Iphone,Uitableview,我在申请中使用以下方法: - (UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath 在这个方法中,通过使用indexPath.row我可以得到每行的行号 但我想实际访问该行的单元格,并仅对该单元格进行格式化 请指导我。您可以使用UITableView-cellforrowatinexpath方法访问一个可见的单元格。参考文献: - (UITableVi
- (UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath
在这个方法中,通过使用indexPath.row
我可以得到每行的行号
但我想实际访问该行的单元格,并仅对该单元格进行格式化
请指导我。您可以使用UITableView
-cellforrowatinexpath
方法访问一个可见的单元格。参考文献:
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
返回值表示 表格的单元格,如果单元格 不可见或indexPath不可用 射程
但是(IMO)
cellforrowatinexpath
是设置单元格的最佳位置。可能存在误解。您引用的方法应该告诉tableView在该索引处显示哪一行。因此,每当tableView请求此方法时,它都会这样做,因为此时还没有行
模板代码将生成一个新单元格或将其出列:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
然后,您可以访问单元格
,以配置每个单元格的内容或将内容添加到contentView
如果您在每行的基础上操作单元格的格式(例如,cell.textLabel的颜色),则应使用以下委托方法执行此操作:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
虽然其他答案可以正常工作,但它只会改变用户点击的单元格。对于我的情况,我想改变其他细胞以及。在四处搜索之后,我尝试:
[[tableView cellForRowAtIndexPath:5 setAccessoryType:UITableViewCellAccessoryNone];
这不起作用,因为IndexPath不是整数。(但是,indexpath.row是)。
因此,在查找文档时,我发现可以使用nsindepath将一行编码为
nsindepath值。以下是工作代码:
[[tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:5 inSection:0]] setAccessoryType:UITableViewCellAccessoryNone];
这样,只要有tableview对象,就可以在任何位置、任何行/节访问单元格
希望这对其他人有所帮助。cellForRowAtIndexPath仅适用于可见单元格。绝对不是任何地方,任何行/段。