【发布时间】:2014-03-09 09:34:58
【问题描述】:
在下面的代码中,我试图在所选行上显示图像并从先前选择的行中删除图像。在上下滚动表格视图时,所选图像也开始出现在随机行上,而不是仅显示在所选行上。任何人都可以在这里帮我解决它。我正在 ios7 设备上测试这个。 谢谢。
- (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];
UIImage *image = [UIImage imageNamed:@"btn_checkmark_off.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(270, 15, 17, 17);
imageView.tag = CELL_IMGVIEW_TG;
[cell.contentView addSubview:imageView];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.prevRowIndex != indexPath.row)
{
NSIndexPath *pPrevIndexPath = [NSIndexPath indexPathForRow:self.prevRowIndex inSection:indexPath.section];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:pPrevIndexPath];
UIImageView* pImgView = (UIImageView*)[cell.contentView viewWithTag:CELL_IMGVIEW_TG];
pImgView.image = [UIImage imageNamed:@"btn_checkmark_off.png"];
// update index
self.prevRowIndex = indexPath.row;
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView* pImgView = (UIImageView*)[cell.contentView viewWithTag:CELL_IMGVIEW_TG];
pImgView.image = [UIImage imageNamed:@"btn_checkmark_on.png"];
}
【问题讨论】:
标签: iphone uitableview ios5 ios7