iOS自定义单元格 - 重用标识符

时间:2015-05-01 11:11:17

标签: ios

我正在尝试创建一个带有收藏夹按钮的自定义单元格。它工作正常,但我认为重用标识符在这里产生问题。我已经用两种方式尝试了它,它在两者中都运行良好。

这是我的第一次尝试: 当我选择按钮时,它将变为黄色按钮,反之亦然。但在向上或向下滚动后,它会再次变为正常状态。为什么呢?

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CI=@"CELL";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CI];
    if(!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CI];
    }
    favbtn=[[UIButton alloc]initWithFrame:CGRectMake(160, 10, 30, 35)];

    [cell.contentView addSubview:favbtn];
    [favbtn setBackgroundImage:[UIImage imageNamed:@"starr.png"] forState:UIControlStateNormal];
    [favbtn addTarget:self action:@selector(fav:) forControlEvents:UIControlEventTouchUpInside];
    cell.textLabel.text=[favdata objectAtIndex:indexPath.row];
    return cell;

}

-(void)fav:(id)sender
{
    if ([sender isSelected]) {
        [sender setImage:[UIImage imageNamed:@"starr.png"]  forState:UIControlStateNormal];
        [sender setSelected:NO];
    } else {
        [sender setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateSelected];
        [sender setSelected:YES];
    }
}

这是我的第二次尝试:

这里每六个细胞显示相同的状态。

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CI=@"CELL";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CI];
    if(!cell) {

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CI];

        favbtn=[[UIButton alloc]initWithFrame:CGRectMake(160, 10, 30, 35)];

        [cell.contentView addSubview:favbtn];
        [favbtn setBackgroundImage:[UIImage imageNamed:@"starr.png"] forState:UIControlStateNormal];
        [favbtn addTarget:self action:@selector(fav:) forControlEvents:UIControlEventTouchUpInside];
        cell.textLabel.text=[favdata objectAtIndex:indexPath.row];
    }

    return cell;
}

1 个答案:

答案 0 :(得分:1)

首先修改此方法:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CI=@"CELL";
    UIButton * favbtn;
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CI];
    if(!cell)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CI];
        favbtn=[[UIButton alloc]initWithFrame:CGRectMake(160, 10, 30, 35)];
        [cell.contentView addSubview:favbtn];
        [favbtn setTag:11221133];
    }

    favbtn = [cell.contentView viewWithTag:11221133];
    [favbtn setBackgroundImage:[UIImage imageNamed:@"starr.png"] forState:UIControlStateNormal];
    [favbtn setImage:[UIImage imageNamed:@"starr.png"]  forState:UIControlStateNormal];
    [favbtn setImage:[UIImage imageNamed:@"star.png"]  forState: UIControlStateSelected];

    //Check selections here and if this index is selected make btn selected.

    [favbtn addTarget:self action:@selector(fav:) forControlEvents:UIControlEventTouchUpInside];
    cell.textLabel.text=[favdata objectAtIndex:indexPath.row];
    cell.contentView.tag = indexPath.row;
    return cell;

}

-(void)fav:(id)sender
{
    UIButton * btn = (UIButton *)sender;
    btn.selected = !btn.selected;
    int index = [[btn superview] tag]; // use this index for storing selections
}

然后创建一个用于存储选择的数组。比较cellForRowAtIndexPath中的这个选项,并选择或不选择btn。

希望这会对你有帮助.....