【发布时间】: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];
}
}
这段代码没有什么花哨的。我怎样才能以编程方式解决这个问题..谢谢..!
【问题讨论】:
-
是的。你有什么解决办法???
-
在 cellforrowatindex 中使用与 IBAction 中相同的方法
标签: ios objective-c uitableview uibutton