滚动时表视图返回错误值 [英] Table view return wrong values when scrolling
问题描述
问题是我已经实现了一个带有故事板自定义单元格的表格视图.每个单元格都有一个按钮作为选择按钮,这意味着每当我按下按钮时,图像就会改变.例如,如果我按下单元格索引 = 0 的按钮,它的图像会正确更改,但是在滚动表格时我发现其他按钮也更改了它们的图像!问题来自表索引路径,我花了很多时间试图修复它但没有结果.有什么解决办法吗?
The problem is that I have implemented a table view with custom cell from storyboard. Each cell has a button as a selection button which mean whenever I press on a button , it image change. For example, if I press on the button with cell index=0, it image change correctly however when scrolling the table I found other buttons also changed their images! The problem is from the table index path and I spent a lot of time trying to fix it with no result. Any Solution?
static NSString *simpleTableIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
NSLog(@"index path inside cell = nil %i",(int)indexPath);
}
buttonObj=(UIButton *)[cell viewWithTag:40];
[buttonObj addTarget:self action:@selector(select:) forControlEvents:UIControlEventTouchDown];
return cell;
-(void) select:(id)sender{
UIButton *button=(UIButton *)sender;
UITableViewCell *clickedCell = (UITableViewCell*)[[sender superview] superview];
NSIndexPath *indexPathCell = [addFriendTableView indexPathForCell:clickedCell];
NSLog(@"indexPathCell %i",(int)indexPathCell.row);
Contacts* selectedContact = [contactsArray objectAtIndex:indexPathCell.row];
if([self image:button.imageView.image isEqualTo:[UIImage imageNamed:@"addFriend.png"]]==YES){
[button setImage:[UIImage imageNamed:@"addFriendPressed.png"] forState(UIControlStateNormal)];
[selectFriendsArray addObject:selectedContact];
counter++
}
else if( [self image:button.imageView.image isEqualTo:[UIImage imageNamed:@"addFriendPressed.png"]]==YES)
{
[button setImage:[UIImage imageNamed:@"addFriend.png"] forState:(UIControlStateNormal)];
counter--;
[selectFriendsArray removeObject:selectedContact];
}
推荐答案
这里你要做的是为每个单元格获取相同的 UIButton
对象.您需要为每个单元格创建一个单独的按钮.
Here what you are doing is getting same UIButton
object for every cell. You need to create a separate button for every cell.
用唯一的标签标识这个 UIButton
对象,然后进一步选择该单元格.
Identify this UIButton
object with unique tag and then select that cell for further.
为 UIButton
对象创建一个标签.在 #import
statments
Create a tag for UIButton
object. Put below line at top of your view controller class after #import
statments
#define kButtonTag 1000
现在为每个对象设置唯一的标签.
Now set unique tag for each object.
static NSString *simpleTableIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
NSLog(@"index path inside cell = nil %i",(int)indexPath);
}
// kButtonTag = 1000
NSInteger btnTag = kButtonTag + indexPath.row;
[[cell.contentView viewWithTag:btnTag] removeFromSuperview];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// Set button properties which you required here...
//....
[button setTag:[cell viewWithTag:kButtonTag + indexPath.row]];
[button addTarget:self action:@selector(select:) forControlEvents:UIControlEventTouchDown];
[cell.contentView addSubview:button];
在您的选择方法中,检查该按钮标签.
In your select method, check for that button tag.
- (void) select:(UIButton *)sender {
NSInteger tag = sender.tag;
NSInteger index = tag - kButtonTag;
// Above index is unique index for your cell.
}
希望你能从这个简短的解释中得到想法.
Hope you will get idea, From this short explanation.
这篇关于滚动时表视图返回错误值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!