由于重用标识符

时间:2017-07-11 04:33:08

标签: ios objective-c uitableview

我试图以编程方式在特定单元格上放几个按钮。但我面临的问题是,当我滚动桌面视图时,按钮正在细胞上蔓延......

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

    NSString *cellIdentifier = @"profilecell";


    ProfileTableViewCell *cell = (ProfileTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    if (cell == nil) {

        cell = [[ProfileTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"profilecell"];
    }






    if (indexPath.row == 5 || indexPath.row == 6 || indexPath.row == 11 || indexPath.row == 12 || indexPath.row == 13) {

        int tag = indexPath.row;

        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, cell.valueTextView.bounds.size.width, cell.valueTextView.bounds.size.height)];
        button.backgroundColor = [UIColor greenColor];
        button.tag = tag;
        [button addTarget:self action:@selector(getTheList:) forControlEvents:UIControlEventTouchUpInside];

        [cell.valueTextView addSubview:button];


    }



    if (indexPath.row == 14 || indexPath.row == 15) {

            int tag = indexPath.row;

                UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, cell.valueTextView.bounds.size.width, cell.valueTextView.bounds.size.height)];
                button.tag = tag;
                [button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];

                [cell.valueTextView addSubview:button];


    }



    return cell;
}

我尝试按下按钮颜色,当第一次显示正确的按钮位置时加载tableview。但滚动后按钮出现在不同的indexPaths中。

任何人都可以告诉我这个解决方案.....

4 个答案:

答案 0 :(得分:0)

细胞被重复使用。如果按钮存在,您需要else删除按钮。

永远不要使用单元格的indexPath作为按钮的标记。如果可以删除,插入或移动表视图中的行,则会严重失败。

在按钮操作方法中,您可以从按钮的框架中确定indexPath,而不是其标签。

答案 1 :(得分:0)

您可以在UITableViewCell类中实现cellForReuse()函数,并在每次重用时为该按钮设置默认行为。

答案 2 :(得分:0)

您应该重复使用按钮,而不是始终创建新按钮。这样做:

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

    NSString *cellIdentifier = @"profilecell";

    ProfileTableViewCell *cell = (ProfileTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    UIButton *button;

    if (cell == nil) {
        cell = [[ProfileTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"profilecell"];
        button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, cell.valueTextView.bounds.size.width, cell.valueTextView.bounds.size.height)];
        button.backgroundColor = [UIColor greenColor];
        button.tag = indexPath.row;
        [cell.valueTextView addSubview:button];
    } else {
        button = (UIButton *)[cell.valueTextView viewWithTag:indexPath.row];
    }

    if (indexPath.row == 5 || indexPath.row == 6 || indexPath.row == 11 || indexPath.row == 12 || indexPath.row == 13) {

        [button addTarget:self action:@selector(getTheList:) forControlEvents:UIControlEventTouchUpInside];

    }



    if (indexPath.row == 14 || indexPath.row == 15) {
        [button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];

    }

    return cell;
}

答案 3 :(得分:0)

对于单元格重用,当我们在加载另一个类型子视图之前重置contentview时动态添加子视图可以正常工作,这样在添加另一组子视图之前,应先删除现有视图,这会产生不必要的复杂性。

但是继承UITableViewCell&重复使用最小的文本更改/属性更改始终被认为是最好的方法。它还将改进表格和表格的滚动。你的代码将更具可读性。

子类

@interface ProfileTableViewCell : UITableViewCell
{
    IBOutlet UIButton *button;
}
-(void)updateButtonLayout:(int)index;
@end

@implementation ProfileTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)updateButtonLayout:(int)index
{

    if (index == 5 || index == 6 || index == 11 || index == 12 || index == 13) {
        //set color
    }
    if (index == 14 || index == 15) {
        //set frame
    }
}
-(IBAction)getTheList:(UIButton *)btn
{
    //add functionality
}

@end


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

    NSString *cellIdentifier = @"profilecell";


    ProfileTableViewCell *cell = (ProfileTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    if (cell == nil) {

        cell = [[ProfileTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"profilecell"];
    }
[cell updateButtonLayout:indexPath.row];
return cell
}