(IOS)当我想在单元格中禁用按钮时,它会使许多其他禁用

时间:2014-05-30 00:20:26

标签: ios button tags sender

我正在制作ios应用。我的问题是当我想在单元格中禁用按钮时,它会使许多其他禁用。我尝试了很多问题,但我真的不知道为什么。按钮的标签是好的,我想我没有正确使用发件人。我的代码看起来像这样:

- (IBAction) fiyeItPressed:(UIButton*)sender
{
    UIButton *fiyeItButton = (UIButton*) sender;

    [fiyeItButton setEnabled:NO];
    NSLog(@"%d",[fiyeItButton tag]);

    NSString *FiyeNumberString = [playlistVote objectAtIndex:sender.tag];
    int FiyeInt = [FiyeNumberString intValue] + 1;
    FiyeNumberString = [NSString stringWithFormat:@"%d",FiyeInt];
    [playlistVote  replaceObjectAtIndex:sender.tag withObject:FiyeNumberString];

    [self saveForTable];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    PlaylistTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.SongName.text = [playlistSongTitle objectAtIndex:indexPath.row];
    cell.ArtisteName.text = [playlistSongArtist objectAtIndex:indexPath.row];
    cell.numberOfVote.text = [playlistVote objectAtIndex:indexPath.row];
    cell.VoteButton.tag = indexPath.row;

    return cell;
}

2 个答案:

答案 0 :(得分:0)

由于您正在重复使用TableView单元格,这意味着当您开始在TableView中滚动时,具有禁用按钮的单元格最终将被重用并显示。您需要做的是在CellForRowAtIndexPath中主动将按钮设置为启用或禁用。

我建议您跟踪按下按钮的索引,然后使用该信息确定是否应在CellForRowAtIndexPath方法中启用或禁用该按钮。

答案 1 :(得分:0)

看看你已经拥有的东西,似乎最简单的“快速修复”就是拥有另一个像playlistSongTitle等数组。

所有它需要做的是有一些bool来跟踪按钮是否被启用。然后在你的cellForRowAtIndexPath:方法中你需要的只是。

[cell.voteButton setEnabled:[playlistButtonsEnabled objectAtIndex:indexPath.row]];

您可能需要先提取bool,不确定它是否能正确推断出类型。 然后在fiyeItPressed:方法中将BOOL调整为YES或NO。

希望这有帮助