在我的应用程序中,我的一个 UIView
中有 UITableView
。我为所有单元格添加了背景图像。我的问题是,当我选择一个特定的单元格时,我想单独更改该特定单元格的背景图像,而所有其他单元格都应该有一个旧的背景图像。
我怎样才能做到这一点。请分享您的想法。
这是我的cellForRowAtIndexPath
代码
- (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];
}
// Configure the cell.
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MenuItem3"]];
cell.backgroundView = bgView;
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.text = [arrayValue objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryNone];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
最佳答案
无需重新加载所有表格,只需像这样重新加载 PreviousInxed
- (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];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bck.png"]];
UITableViewCell *celld = (UITableViewCell *)[tableView cellForRowAtIndexPath:table.indexPathForSelectedRow];
celld.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"raj_star.png"]];
return cell;
}
NSIndexPath *perviouseIndexPathaf;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (perviouseIndexPathaf)
{
[table reloadRowsAtIndexPaths:[NSArray arrayWithObject:perviouseIndexPathaf]
withRowAnimation:UITableViewRowAnimationNone];
}
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"raj_star.png"]];
perviouseIndexPathaf = indexPath;
}
关于iphone - 如何从 UITableViewCell 中删除背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13796761/