ios 7:滚动时 UITableViewCell 自定义编辑按钮选择问题?
Posted
技术标签:
【中文标题】ios 7:滚动时 UITableViewCell 自定义编辑按钮选择问题?【英文标题】:ios 7 : UITableViewCell custom edit button selection issue while scrolling? 【发布时间】:2014-08-28 09:16:55 【问题描述】:我在 UITableView 中使用自定义单元格来显示记录。我在编辑和制作用于选择和取消选择的按钮时自定义了自定义单元格删除按钮。这里的问题是在滚动表格视图时,我在按钮上得到重复的选择和取消选择。我也分享了选择的图像。
请告诉我在选择和取消选择 UITableViewCell 时我必须在哪里处理滚动问题
提前致谢!
【问题讨论】:
请把你的代码放在这里cellForRowAtIndexPath
方法
【参考方案1】:
您可以检查选择并替换按钮图像或图像
imgIconNormal = [UIImage imageNamed:@"icon_mark.png"];
imgIconSelected = [UIImage imageNamed:@"icon_mark_selected.png"];
在 CellForRowAtindexPath 中
BOOL selected = [[tableView indexPathsForSelectedRows] containsObject:indexPath];
UIImageView *img = (UIImageView *)[cell viewWithTag:1];
img.image = selected ? imgIconSelected : imgIconNormal;
在选择时
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView *img = (UIImageView *)[cell viewWithTag:1];
img.image = imgIconSelected;
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView *imgTrackSelection = (UIImageView*)[cell viewWithTag:1];
imgTrackSelection.image = imgIconNormal;
根据您的组件修改您的代码。我用过 UIImageView
【讨论】:
我在 UITableViewCell 类中添加自定义按钮,因为我必须在编辑单元格时替换按钮,并且在 cellforrowatIndex 方法中找不到状态 isEditing。 我认为你必须在你的问题中粘贴你的 cellForRowAtIndexPath 代码。 是的,你的逻辑是正确的。它奏效了。由于我使用相同的模型对象来填充表,因此我使用了一个数组来存储所有对象并比较它 cellforRowatIndex 数组是否包含已填充的对象。并相应地设置状态【参考方案2】:在添加自定义按钮之前使用它:
for (UIButton *btn in [cell.contentView.superview subviews])
if([btn isKindOfClass:[UIButton class]])
[btn removeFromSuperview];
【讨论】:
以上是关于ios 7:滚动时 UITableViewCell 自定义编辑按钮选择问题?的主要内容,如果未能解决你的问题,请参考以下文章
iOS UITableViewCell 内容在第一次滚动时移动
在 UITableviewCell (ios7) 中嵌入的 UITableView 中滚动
iOS7 在 UITableViewCell 中以适当的偏移量编辑 UITextField 时滚动到 UITableViewController 的行