UITableView使用按钮图像在单元格上添加按钮以及如何重置按钮图像

时间:2014-12-12 09:55:24

标签: ios uitableview

如何在UITableView单元格中添加带图像的按钮,以及如何在单击该按钮时更改图像以及在单击iOS中的其他行按钮时如何重置按钮图像

2 个答案:

答案 0 :(得分:1)

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


// configure cell

//create button

    UIButton *checkUncheckButton = [UIButton buttonWithType:UIButtonTypeCustom];
     mailLable.frame = CGRectMake(80, 27, 250, 25);
    [checkUncheckButton setImage:[UIImage imageNamed:@"uncheckednew.png"] forState:UIControlStateNormal];
     checkUncheckButton.backgroundColor = [UIColor clearColor];
     [checkUncheckButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
     checkUncheckButton.userInteractionEnabled = YES;
     [checkUncheckButton addTarget:self action:@selector(checkUncheckButton:) forControlEvents:UIControlEventTouchUpInside];
     checkUncheckButton.tag = indexPath.row;
     [cell.contentView addSubview:checkUncheckButton];

}

#pragma mark - checkUncheckButton action

    -(void)checkUncheckButton:(id)sender{

        UIButton *button = (UIButton *)sender;
        NSLog(@"%i",button.tag);

        if([button.currentImage isEqual:[UIImage imageNamed:@"uncheckednew.png"]])
        {
            [button setImage:[UIImage imageNamed:@"checkednew.png"] forState:UIControlStateNormal];

        }
        else
        {
            [button setImage:[UIImage imageNamed:@"uncheckednew.png"] forState:UIControlStateNormal ];

        }
    }

我认为可以使用上面的代码。

答案 1 :(得分:1)

您必须创建自定义UITableViewCell,您必须在其中设置要显示的图像。 帮助链接: -

Custom Check mark for selected Row

http://www.appcoda.com/customize-table-view-cells-for-uitableview/