Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.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 由于重复使用标识符,滚动时,特定单元格上的UI按钮分布在不同的单元格上_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 由于重复使用标识符,滚动时,特定单元格上的UI按钮分布在不同的单元格上

Ios 由于重复使用标识符,滚动时,特定单元格上的UI按钮分布在不同的单元格上,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我试图通过编程在特定的单元格上设置几个按钮。但我面临的问题是,当我滚动tableview时,按钮在单元格上扩散 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *cellIdentifier = @"profilecell"; ProfileTableViewCell *cell = (Profi

我试图通过编程在特定的单元格上设置几个按钮。但我面临的问题是,当我滚动tableview时,按钮在单元格上扩散

- (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第一次加载时,它显示按钮的正确位置。但是在滚动之后,按钮以不同的索引路径出现


有人能告诉我解决这个问题的方法吗?…

电池可以重复使用。您需要一个
else
,如果按钮存在,它将删除该按钮

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


在button action方法中,您可以根据按钮的框架而不是其标记来确定indexPath。

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

您应该重用按钮,而不是总是创建新的按钮。这样做:

- (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;
}

对于单元重用,当我们在加载另一类型的子视图之前重置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
}

您的
按钮
接缝具有相同的属性,除了颜色和正确的操作。。!再次将按钮添加为子视图可能会出现问题。尝试将您的
ui按钮添加到
ProfileTableViewCell
中,而不是以编程方式。我不确定。这可能会有帮助