Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 滚动UITableView时,UIButton在UITableView单元格中插入的重复选定状态_Ios_Uitableview - Fatal编程技术网

Ios 滚动UITableView时,UIButton在UITableView单元格中插入的重复选定状态

Ios 滚动UITableView时,UIButton在UITableView单元格中插入的重复选定状态,ios,uitableview,Ios,Uitableview,我在我的UITableViewCell中选择了一个UIButton,它保持选中状态,我为UIButton的每个状态添加了一个图像(通过Interface Builder,而不是通过编程)。但当我滚动时,所选状态显示为重复。我读了很多有类似信息的帖子,但是找不到解决问题的方法。我注意到tableView正在使用ui按钮的状态重用缓存中的单元格 这是我的密码 在我的自定义单元格中: - (IBAction)highlightStar:(UIButton *)sender { self.sta

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

这是我的密码

在我的
自定义单元格中

- (IBAction)highlightStar:(UIButton *)sender {
    self.starButton.selected=!sender.selected;
}
在我的控制器的
viewDidLoad
中:

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

   [self.tableView registerNib:nib
    forCellReuseIdentifier:CellIdentifier];
- (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;
}
- (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

}
在我的控制器的
cellforrowatinexpath
中:

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

   [self.tableView registerNib:nib
    forCellReuseIdentifier:CellIdentifier];
- (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;
}
- (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

}
捕获1(无滚动)。选择两个按钮:

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


谢谢

由于单元格在滚动过程中会被重复使用,您还需要在从
单元格返回之前更新按钮状态。在
自定义单元格
类中,请添加以下内容:

- (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;
}
应该这样做

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

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

   [self.tableView registerNib:nib
    forCellReuseIdentifier:CellIdentifier];
- (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;
}
- (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状态的代码,因为您现在在此处执行此操作。

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

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;

谢谢你,伙计!我很快就开始尝试并告诉您一些事情,但是如何从自定义单元格类中获取我所接触的行的indexPath呢?我试过使用CGPoint按钮position=[sender-convertPoint:CGPointZero-to-view:self.tableView];NSIndexPath*indexPath=[self.tableView indexPathForRowAtPoint:buttonPosition];。我使用在控制器中创建的协议方法获取self.tableView,但它不接收数据。我在init方法中设置了委托。我必须更改自定义单元格中的数据模型,不是吗?在这里我可以更改UIButtons的状态。但是我如何将BOOL状态与自定义单元格中的行关联起来呢?我编辑了代码片段,为您提供了一个示例,说明如何在使用starButton时跟踪单元格的indexPath。请Rufel,您能看到我的新答案吗。感谢和抱歉的道歉;(