當我從代碼理解你創建的UIButton每當UITableView的詢問小區。在這兩種情況下,如果你創建一個新的單元格或者你使用了一個新的單元格。然後,您每創建一個按鈕,再創建一個按鈕。移動按鈕創建到細胞創造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的計數相同。
你有沒有想過如何解決這個問題?我現在有同樣的問題。 – Foo