iphone - 如何从 UITableViewCell 中删除背景图像?

标签 iphone ios uitableview

在我的应用程序中,我的一个 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/

相关文章:

iphone - applicationProtectedDataWillBecomeUnavailable : application delegate method also apply to the keychain?

ios - 不推荐使用 Objective-C 选择器的字符串文字,改用 '#selector'

ios - 错误 ITMS-90047 iTunes Connect

ios - 有没有办法使用 RubyMotion 在 UITableViewController 中重新加载数据?

ios - 自定义 TableView 单元格重用代码审查问题

ios - 利用加速度计检测敲击压力强度

iphone - 在opengl ES中画一个箭头

ios - 如何为 UICollectionView 实现 UITableView 的滑动删除

android - 能源经济 : BLE device - when to make connectable?

objective-c - 如果表格 View 为空则显示标签