ios - UITableViewCell 添加按钮

标签 ios uitableview

我想在自定义单元格中添加一些按钮,当我点击按钮时,其他按钮也会发生变化。如何在不更改其他按钮的情况下单击一个按钮?

enter code here
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    SViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[SViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault   
        reuseIdentifier:identifier];
    }
    cell.cellButton.tag = indexPath.row;
    [cell.cellButton addTarget:self action:@selector(clickMe:) 
         forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

- (void)clickMe:(UIButton *)sender
{
    SViewTableViewCell *cell = (SViewTableViewCell *)[[sender superview]superview];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    if (sender.tag == 0) {
        if ([sender.titleLabel.text isEqualToString:openStr]) {
             [sender setTitle:closeStr forState:UIControlStateNormal];
        }
        else{
             [sender setTitle:openStr forState:UIControlStateNormal];
        }
    }
}

最佳答案

我认为这是关于 tableView 单元格重用的问题,您应该在 cellForRow 中设置按钮标题以保持重用单元格具有正确的标题,您应该尝试这样的代码:

openDict 是您应该定义的 NSMutableArray,这只是一个示例。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    SViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[SViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault   
        reuseIdentifier:identifier];
        [cell.cellButton addTarget:self action:@selector(clickMe:) 
         forControlEvents:UIControlEventTouchUpInside];
    }

    NSString *key = [NSString stringWithFormat:@"%d-%d", indexPath.row, indexPath.section];        
    if (openDict[key] && [openDict[key] boolValue]) {
        [sender setTitle:openStr forState:UIControlStateNormal];
    }
    else{
        [sender setTitle:closeStr forState:UIControlStateNormal];
    }

    cell.cellButton.tag = indexPath.row;
    return cell;
}

- (void)clickMe:(UIButton *)sender
{
    SViewTableViewCell *cell = (SViewTableViewCell *)[[sender superview]superview];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    if (sender.tag == 0) {
        NSString *key = [NSString stringWithFormat:@"%d-%d", indexPath.row, indexPath.section];        
        if (openDict[key] && [openDict[key] boolValue]) {
             openDict[key] = @(0);
             [sender setTitle:closeStr forState:UIControlStateNormal];
        }
        else{
             openDict[key] = @(1);
             [sender setTitle:openStr forState:UIControlStateNormal];
        }
    }
}

关于ios - UITableViewCell 添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22677201/

相关文章:

ios - 如何关闭 Popoverviewcontroller?

TableView Controller 中的 iOS/AFNetworking TableView 不显示从响应对象填充的数组中的任何数据

ios - 内容在 UITableViewCell 上显示不正确

ios - 当单元格离开屏幕时,Tableview 单元格内容会更改

ios - 配置文件不包含证书

ios - 使用 CGFloat.max 调整标签大小,移动它

ios - NSAttributedString 报告 UITextView sizeThatFits 和 boundingRectWithSize 的大小不正确,并设置了正确的选项

ios - 如何在使用默认单元格值加载一次 View 后更新 UITableView 单元格(cell.detailTextLabel.text)的值

ios - 选择行时禁用 uitableviewcell 中的突出显示按钮

ios - 如何让自定义 UITableViewCell 获取用户对首选文本大小的更改?