Scroll UITableView时,在UITableViewCell中插入UIButton的重复选定状态

时间:2013-12-10 17:54:52

标签: ios uitableview

我在UIButton中选择UITableViewCell并保持选中状态,我已为UIButton的每个状态添加了一个图像(通过Interface Builder,而不是以编程方式)。但是当我滚动时,所选状态显示重复。我已经阅读了很多类似信息的帖子,但无法找到问题的解决方案。我注意到tableView正在使用UIButton的状态重用缓存中的单元格。

这是我的代码。

在我的Custom Cell

- (IBAction)highlightStar:(UIButton *)sender {
    self.starButton.selected=!sender.selected;
}

在我的控制器viewDidLoad中:

UINib *nib = [UINib nibWithNibName:@"CellS"
                                bundle:nil];

   [self.tableView registerNib:nib
    forCellReuseIdentifier:CellIdentifier];

在我的控制器cellForRowAtIndexPath中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    CellS *cell = (CellS *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];



    cell.backgroundColor = [UIColor clearColor];

    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

    NSDictionary *dict=self.items[indexPath.row];

    NSString *header=[dict objectForKey:@"header"];

    [cell.labelReader setText:header];



    return cell;
}

捕获1(不使用滚动)。选择了两个UIButton:

enter image description here

捕获2(使用滚动)。选择了两个重复的UIButton:

enter image description here

由于

3 个答案:

答案 0 :(得分:0)

由于单元格在滚动过程中被重复使用,因此您需要在从cellForRowAtIndexPath

返回之前更新按钮状态

答案 1 :(得分:0)

Custom Cell课程中,添加以下内容:

- (void)prepareForReuse
{
    self.starButton.selected= NO;
}

同样在你的cellForRowAtIndexPath中,你应该更新starButton状态:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    SBQCell *cell = (SBQCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.backgroundColor = [UIColor clearColor];

    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

    NSDictionary *dict=self.items[indexPath.row];

    NSString *header=[dict objectForKey:@"header"];

    [cell.labelReader setText:header];

    // HERE - Update cell's starButton state according to your current data info
    cell.starButton.selected = [dict objectForKey:@"selectedState"];

    // Keep track of indexPath.row and add target
    cell.starButton.tag = indexPath.row;
    [cell.starButton addTarget:self action:@selector(didTapStarButton:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

应该这样做。

编辑:我添加了一种方法来跟踪你的starButton在cellForRowAtIndexPath中的状态。您需要在viewController中添加以下内容:

- (void)didTapStarButton:(UIButton *)button
{
    button.selected = !button.selected;
    // Here, keep track of the change in state with button.tag that is equal to the cell's indexPath.row

}

您可以删除自定义单元格中更改starButton状态的代码,因为您现在可以在此处执行此操作。

答案 2 :(得分:0)

我找到了解决方案!更改了我的模型,每次更改收藏状态时,我都会更改模型,并从模型中更新单元格。

cellForRotAtIndexPath

Model *new=nil;
//I parse the plist, and fill an array with Model Objects, where each Object is an item from the Plist. The method newsAtIndex returns the Model Object for the selected index   
new=[self.model newsAtIndex:indexPath.row];
cell.labelReader.text=new.header;
[cell.starButton addTarget:self action:@selector(didTapStarButton:)      forControlEvents:UIControlEventTouchUpInside];
cell.starButton.tag = indexPath.row;
cell.starButton.selected=new.Favorite;

didTapStarButton

Model *new=nil;
new=[self.model newsAtIndex:button.tag];
new.Favorite=button.selected;
相关问题