正如我从代码中了解到的那样,每次 UITableView 要求单元格时,您都会创建 UIButton 。在这两种情况下,如果您创建一个新单元格或使用出列单元格。然后,您每次在已创建的按钮上创建并添加一个按钮。将按钮创建移动到单元格创建if
所以你的cellForRowAtIndexPath
方法应该看起来像
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [self tableviewCellWithReuseIdentifierFollowing:CellIdentifier];
followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[followingButton addTarget:self action:@selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];
[followingButton setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];
followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);
[cell.contentView addSubview:followingButton];
}
NSLog(@"row--%d",indexPath.row);
followingButton.tag=indexPath.row;
NSLog(@"followingButton.tag--%d",followingButton.tag);
[self configureCellFollowing:cell forIndexPath:indexPath];
return cell;
}
但这不会是您问题的竞争解决方案。您可能知道 UITableView 使用“可重用”单元格来减少系统内存使用量。它仅使用当前需要显示的单元格数量。因此,在具有 100 个单元格的表格中,它实际上会创建大约 10 个。并且所有这些单元格都不会存储所有按下/未按下按钮的正确状态。为了实现正确的行为,您必须拒绝使用标签并改用一些模型逻辑。最简单的方法 - NSMutableArray,您将在其中存储按钮状态。在 followingButtonpressed:
方法中,您将正确的对象设置为 YES/NO,然后cellForRowAtIndexPath
您将读取此值。检查下面的代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [self tableviewCellWithReuseIdentifierFollowing:CellIdentifier];
followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[followingButton addTarget:self action:@selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];
[followingButton setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];
followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);
[cell.contentView addSubview:followingButton];
BOOL isSelected = [[statesArray objectAtIndex:btnPly.tag] boolValue];
[self setState:isSelected forButton:followingButton];
}
NSLog(@"row--%d",indexPath.row);
followingButton.tag=indexPath.row;
NSLog(@"followingButton.tag--%d",followingButton.tag);
[self configureCellFollowing:cell forIndexPath:indexPath];
return cell;
}
-(void)followingButtonpressed:(id)sender
{
NSLog(@"sender tag --%d",[sender tag]);
UIButton *btnPly = (UIButton *)sender;
BOOL isSelected = [[statesArray objectAtIndex:btnPly.tag] boolValue];
[statesArray replaceObjectAtIndex:btnPly.tag withObject:[NSNumber numberWithBool:!isSelected]];
if(isSelected)
{
[self setState:NO forButton:btnPly];
}
else
{
[self setState:YES forButton:btnPly];
}
}
- (void) setState:(BOOL)state forButton:(UIButton *)button
{
if(state)
{
[button setSelected:NO];
[button setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];
}
else
{
[button setSelected:YES];
[button setImage:[UIImage imageNamed:@"following_off12.png"] forState:UIControlStateNormal];
}
}
statesArray
在哪里
NSMutableArray *statesArray = [NSMutableArray new];
你必须在你班级的某个地方创建和初始化它。中的对象statesArray
计数必须与单元格 int tableView 的计数相同。