我在我的应用程序中使用以下方法:
- (UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath
在此方法中,使用indexPath.row
我可以获得每行的行号。
但我想实际访问该行的单元格并仅对该单元格进行一些格式化。
请指导我。
答案 0 :(得分:6)
虽然其他答案可以正常工作,但它只会更改用户点击的单元格。对于我的情况,我也想改变其他细胞。搜索后,我尝试了:
[[tableView cellForRowAtIndexPath:5 setAccessoryType:UITableViewCellAccessoryNone];
这不起作用,因为IndexPath不是整数。 (然而,indexpath.row)。 因此,在查找文档时,我发现我们可以使用NSIndexPath对行进行编码 NSIndexPath值。这是工作代码:
[[tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:5 inSection:0]] setAccessoryType:UITableViewCellAccessoryNone];
这样,只要你有tableview对象,就可以访问ANYWHERE,AT ANY ROW / SECTION中的单元格。
希望这有助于某人。
答案 1 :(得分:2)
也许存在误解。您引用的方法应该告诉tableView在该indexPath上显示哪一行。因此,每当tableView请求此方法时,它都会这样做,因为此indexPath上没有行。
模板代码将生成一个新单元格或出列一个:
- (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];
}
然后,您可以访问cell
来配置每个单元格的内容或向contentView添加内容。
如果您基于每行操作单元格的格式(例如,cell.textLabel的颜色),您应该在以下委托方法中执行此操作:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
答案 2 :(得分:1)
您可以使用UITableView -cellForRowAtIndexPath
方法访问可见单元格。来自UITableView参考:
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
返回值
表示a的对象 表格的单元格或者如果是单元格则为零 不可见或indexPath不可用 范围。
然而(IMO)cellForRowAtIndexPath
是进行单元格设置的最佳位置。