当它被选中时更改单元格的图像
问题描述:
在我的UITableView中,我需要获取用户的事件,所以当单元格被选中(选中)时,我把btn_checked.png
当它未选中时,我把btn_unchecked.png
。当它被选中时更改单元格的图像
我猜,我尝试做应该在didSelectRowAtIndexPath
方法,所以这是我的代码:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *listData =[self->tableContents objectForKey:
[self->sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btn_unchecked.png"]];
theCell.accessoryView = imageView;
}
我的代码段不能正常工作,所以btw_unchecked.png
没有放置于行被检查,而且,我需要获得选中/取消选中的事件以将正确的图像放到正确的事件中。 Thanx提前。
答
您可以使用以下代码。
[cell.contentView addSubview:imageView];
我希望这将帮助你..
答
作为一个例子,这个代码片段将切换单元的对号,当细胞被窃听。该模式是在调用代理方法时强制从表数据源中进行选择性重新加载。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Dequeue the cell... then:
static NSString *CellIdentifier = @"tvcOEList";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.accessoryType = (cell.accessoryType == UITableViewCellAccessoryNone) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}