【问题标题】:UITableView Scrolling removes the UIButton valueUITableView 滚动移除 UIButton 值
【发布时间】:2015-06-26 18:12:48
【问题描述】:

我有一个 UITableview 并添加了 UIButton 作为自定义视图。当我按下按钮时,它会更改所选按钮的标题,但当我滚动它时,它会恢复正常状态。请参阅下面的代码...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    static NSString *Cellidentifier = @"DataTableCellId";
    STCustomCell *cell = (STCustomCell *) [tableView dequeueReusableCellWithIdentifier:Cellidentifier];
    if (cell == nil) {

        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CellView" owner:self options:nil];
    cell = nib[0];

        cellButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        cellButton.tag=indexPath.row;
        [cellButton addTarget:self
                   action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
        [cellButton setTitle:@"Save" forState:UIControlStateNormal];
        cellButton.frame = CGRectMake(265.0, 20.0, 45.0, 27.0);
        cellButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
        cellButton.tintColor = [UIColor lightGrayColor];
        cellButton.titleLabel.font = [UIFont systemFontOfSize:12.0];
        cellButton.layer.borderWidth = 1.0f;
        cellButton.layer.cornerRadius = 5;
        cellButton.layer.masksToBounds = YES;
        [cell.contentView addSubview:cellButton];

    }

    return cell;
}

-(void)buttonClicked:(UIButton*)sender
{      
        [sender setBackgroundColor:[UIColor lightGrayColor]];
        [sender setTintColor: [UIColor whiteColor]];
        [sender setTitle:@"Saved" forState:UIControlStateNormal];
        [sender addTarget:self
                   action:@selector(saveCell:) forControlEvents:UIControlEventTouchUpInside];
        sender.enabled = NO;
}

【问题讨论】:

    标签: ios objective-c xcode uitableview uibutton


    【解决方案1】:

    首先,检查您的子句是否为 nil,检查您的单元格是否为 nil 并且不要实例化它是没有意义的。并且由于可重用行为,您的按钮会进入初始状态。所以,检查你的按钮的 enabled 属性

       {
        NSMutableDictionary *buttonsState;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        buttonsState = [NSMutableDictionary new];
    }
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
        static NSString *Cellidentifier = @"DataTableCellId";
        STCustomCell *cell = (STCustomCell *) [tableView dequeueReusableCellWithIdentifier:Cellidentifier];
        UIButton *cellButton = (UIButton*)[cell.contentView viewWithTag:indexPath.row];
        if (cell == nil) {
    
            cell = [[STCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cellidentifier]
        }
        if (cellButton == nil)
        {
            cellButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            cellButton.tag=indexPath.row;
            [cell.contentView addSubview:cellButton];
    
        }
    
        if ([buttonsState objectForKey:@(indexPath.row)] == nil)
        {
            [cellButton setTitle:@"Save" forState:UIControlStateNormal];
            cellButton.frame = CGRectMake(265.0, 20.0, 45.0, 27.0);
            cellButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
            cellButton.tintColor = [UIColor lightGrayColor];
            cellButton.titleLabel.font = [UIFont systemFontOfSize:12.0];
            cellButton.layer.borderWidth = 1.0f;
            cellButton.layer.cornerRadius = 5;
            cellButton.layer.masksToBounds = YES;
            [cellButton addTarget:self
                           action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
        }
        else
        {
            [cellButton setBackgroundColor:[UIColor lightGrayColor]];
            [cellButton setTintColor: [UIColor whiteColor]];
            [cellButton setTitle:@"Saved" forState:UIControlStateNormal];
            [cellButton addTarget:self
                           action:@selector(saveCell:) forControlEvents:UIControlEventTouchUpInside];
        }
        return cell;
    }
    
    -(void)buttonClicked:(UIButton*)sender
    {
        [buttonsState setObject:@YES forKey:@(sender.tag)];
    
        [sender setBackgroundColor:[UIColor lightGrayColor]];
        [sender setTintColor: [UIColor whiteColor]];
        [sender setTitle:@"Saved" forState:UIControlStateNormal];
        [sender addTarget:self
                   action:@selector(saveCell:) forControlEvents:UIControlEventTouchUpInside];
        sender.enabled = NO;
    }
    

    【讨论】:

    • 感谢您的评论,但是当我添加 UIButton *cellButton = [cell.contentView viewForTag:indexPath.row];我得到一个“uiview”的不可见@interface 声明选择器 viewForTag 警告。
    • 将 viewForTag 更改为 viewWithTag
    • 它仍然给我一个不兼容的指针,并且这个方法不起作用
    • 你是什么意思?你不能编译这个sn-p?我是从头开始写的,所以请告诉我到底哪里出了问题
    • 更改 UIButton cellButton = [cell.contentView viewWithTag:indexPath.row];到 UIButton *cellButton =(UIButton) [cell.contentView viewWithTag:indexPath.row];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多