UITableViewCell添加按钮

时间:2014-03-27 02:31:14

标签: 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];
        }
    }
}

4 个答案:

答案 0 :(得分:1)

我认为这是一个关于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];
        }
    }
}

答案 1 :(得分:1)

请为自定义uitableViewCell

中的每个按钮设置标记
- (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];
    }
    UIButton *button1 = (UIButton *)[cell viewWithTag:1];
    UIButton *button2 = (UIButton *)[cell viewWithTag:2];
    [button1 addTarget:self action:@selector(clickMe:) 
         forControlEvents:UIControlEventTouchUpInside];
   [button2 addTarget:self action:@selector(clickMe:) 
         forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

答案 2 :(得分:0)

要向UITableViewCell添加按钮,您应该在cellForRowAtIndexPath中配置单元格时执行此操作 - 这是在您重复使用单元格之后。如果要向少量单元格添加按钮,则应使用UITableView的reloadRowsAtIndexPaths:withRowAnimation:方法或reloadData重新加载该单元格,以便重新加载所有单元格。

您添加到单元格的任何子视图都应作为子视图添加到单元格的contentView中。您应该删除prepareForReuse中的所有此类视图,或者您可以发现单元格重用会在您不想拥有按钮的单元格中显示额外的按钮。由于需要准备单元格以供重用,因此最好是继承UITableViewCell并在子类上提供方法来添加按钮。

另一种方法是注册多种单元格以便在表格视图中重复使用,根据支持表格视图的数据选择要检索和使用的单元格类型。这就像对每种类型的单元格使用registerClass:forCellReuseIdentifier:一样简单,然后在dequeueReusableCellWithIdentifier:forIndexPath:中应用正确的重用标识符 - 您仍然会对单元格对象进行子类化,但是您在类中进一步配置它们而不是在他们出发的时间。

答案 3 :(得分:0)

使用以下代码:

- (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] superview];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    [self keepSelectionRemainsSameForIndexPath:indexPath andSender:sender];
}

- (void)keepSelectionRemainsSameForIndexPath:(NSIndexPath *)indexPath andSender:(UIButton *)sender {
    if (sender.tag == indexPath.row) {
        if ([sender.titleLabel.text isEqualToString: openStr]) {
            [sender setTitle:closeStr forState:UIControlStateNormal];
        } else {
            [sender setTitle: openStr forState:UIControlStateNormal];
        }
    }
}
相关问题