【问题标题】:UITableviewcell content updating reflectsUITableviewcell 内容更新体现
【发布时间】:2016-05-02 09:22:32
【问题描述】:

我在UITableViewCell 的每一行中添加了三个UIButtons

当我选择UIButton 时,它会将其颜色更改为黑色。问题是这些颜色变化受到了表格中其他单元格按钮的影响,我在向下滚动表格时才看到它。

这个链接也解释了同样的问题->UITableviewcell content updating reflects to other cell

我在 iOS 中找不到此问题的任何答案。

这是我的 cellForRowAtIndexPath

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"custCell"];

    UIButton *btn1=[cell viewWithTag:101];
    btn1.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [btn1 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    btn1.backgroundColor=[UIColor clearColor];
    [btn1 setTag:(indexPath.row*3)+1];

    UIButton *btn2=[cell viewWithTag:102];
    btn2.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [btn2 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    btn2.backgroundColor=[UIColor clearColor];
    [btn2 setTag:(indexPath.row*3)+2];

    UIButton *btn3=[cell viewWithTag:103];
    btn3.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [btn3 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    btn3.backgroundColor=[UIColor clearColor];
    [btn3 setTag:(indexPath.row*3)+3];
rerutn cell;

这是我的按钮操作方法

- (IBAction)btnClicked:(id)sender
{
    UIButton *button=(UIButton *)sender;
    NSLog(@"tag %ld",(long)button.tag);
    if(button.backgroundColor==[UIColor blackColor]){
        button.backgroundColor=[UIColor clearColor];

    }
    else if(button.backgroundColor==[UIColor clearColor]){
        button.backgroundColor=[UIColor blackColor];
    }
}

这段代码没有什么花哨的。我怎样才能以编程方式解决这个问题..谢谢..!

【问题讨论】:

标签: ios objective-c uitableview uibutton


【解决方案1】:

替换

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"custCell"];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"custCell" forIndexPath:indexPath];

        if (cell == nil){

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"custCell"];
        }

希望它会有所帮助:)

【讨论】:

  • 和以前一样。不影响任何人。问题沙爹一样
【解决方案2】:

制作 UITableViewCell 并添加 UIButton 并用故事板标记它们。

@interface TblCellCategoryCell:UITableViewCell

@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;
@property (weak, nonatomic) IBOutlet UIButton *btn3;

@end

【讨论】:

    【解决方案3】:

    在 cellForRowatIndexPth 而不是这一行

    btn1.backgroundColor=[UIColor clearColor];

    使用下面的代码

    if (btn1.isSelected) {
        btn1.backgroundColor=[UIColor clearColor];
    }
    else{
        btn1.backgroundColor=[UIColor blackColor];
    }
    

    在 IBAction 中使用

     UIButton *button=(UIButton *)sender;
     if(button.backgroundColor==[UIColor blackColor]){
        button.backgroundColor=[UIColor clearColor];
        [button setSelected:YES];
    }
    else{
        button.backgroundColor=[UIColor blackColor];
        [button setSelected:NO];
    }
    

    【讨论】:

    • 这不起作用。当 viewWithTag 创建按钮时,我已经更改了按钮的颜色。
    • 你可以在单元格中添加子视图,而不是使用标签值调用,否则你可以使用 tableviewcell 子类
    【解决方案4】:

    这是因为UITableView 重复使用单元格而不是创建新单元格。因此,假设您使用索引 2 更改单元格的颜色,因此每当 UITableView 重用该单元格时,它将显示先前创建的带有更改颜色按钮的单元格。

    对于您的具体问题,您总是在重置按钮的标签,因此下次您尝试从 viewWithTag 获取按钮时,它根本不会返回任何按钮,或者会返回错误的按钮。

    【讨论】:

    • 那么有什么解决办法吗??
    • 您是使用 Storyboard 文件或 nib 文件来创建单元格还是在代码中创建单元格?
    • 我正在使用 StoryBoard.. 如果我以编程方式创建单元格会解决吗
    • 仅使用 Storyboard,创建 UITableViewCell 的子类并使用控件从按钮拖动到 UITableViewCell 子类以在代码中生成按钮的引用,然后使用这些引用而不是使用 viewWithTag
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多