tableview点击变cell背景颜色问题

这篇博客探讨了在UITableView中实现多选时遇到的一个问题:点击一个cell导致其他特定cell背景颜色同步变化。作者提供了相关的代码示例,显示了一个简单的数据填充和表格设置过程,并展示了在`tableView:didSelectRowAtIndexPath:`方法中设置选中cell背景颜色的代码。目前遇到的问题是,当点击一个cell变色后,其他特定位置的cell也会同步变色,需要找到解决方案以实现期望的多选变色效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

功能就是多选效果,点击其中一个 背景变色,在点击就回变回白色,求教
1,一共18个数组,点击第一个cell改变颜色的时候第15个也自动变更颜色,第二个16个也回同事变色,第三个17个也回,
 代码如下
[code=objc]@interface fivetableview ()

@end

@implementation fivetableview
@synthesize dlists;
@synthesize dellist;

NSInteger i;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{

    [super viewDidLoad];
    dellist=[[NSMutableArray alloc]initWithCapacity:30];
    dlists=[[NSMutableArray alloc] initWithCapacity:30 ];
    [dlists addObject:@"大众化的"];
    [dlists addObject:@"线条硬朗的"];
    [dlists addObject:@"流线型的"];
    [dlists addObject:@"大气的"];
    [dlists addObject:@"时尚的"];
    [dlists addObject:@"简约的"];
    [dlists addObject:@"有力量感的"];
    [dlists addObject:@"张扬的"];
    [dlists addObject:@"霸气的"];
    [dlists addObject:@"动感的"];
    [dlists addObject:@"稳重的"];
    [dlists addObject:@"优雅的"];
    [dlists addObject:@"粗犷的"];
    [dlists addObject:@"创新的"];
    [dlists addObject:@"中性的"];
    [dlists addObject:@"活力的"];
    [dlists addObject:@"经典的"];
    NSLog(@"%d",[dlists count]);
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    
    return [dlists count];
    //return [dlists count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCell4";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // Configure the cell...
    NSInteger row = [indexPath row];
    cell.textLabel.text = [self.dlists objectAtIndex:row];
        NSLog(@"row=%d",row);
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 
  
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];

@end
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值