目标C:如何在单元格中添加“加号”按钮

问题描述

|| 谁能建议我如何在UITableView单元格中添加一个“加号”按钮,如下面的屏幕快照所示?     

解决方法

        简单的方法是:获取加号的图像,并将其设置为
cell.imageView.image
。     ,        您还可以将表格视图置于编辑模式,实现
editingStyleForRowAtIndexPath:
,然后返回
UITableViewCellEditingStyleInsert
(这会在绿色圆圈中加一个加号)。 例如...
- (IBAction)editButtonPressed
{
    //toggle editing on/off...
    [tableView setEditing:(!tableView.editing) animated:YES];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
    editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
        return UITableViewCellEditingStyleInsert;
               //gives green circle with +
    else
        return UITableViewCellEditingStyleDelete;
               //or UITableViewCellEditingStyleNone
}
当按下绿色按钮时,表格视图将调用
tableView:commitEditingStyle:forRowAtIndexPath:
- (void)tableView:(UITableView *)tableView 
    commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
    forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleInsert)
    {
        //handle insert...
    }
    else
    {
        //handle delete...
    }
}
    ,        您可以通过获取
UITableViewCell
contentView
属性并添加加号按钮的ѭ8as作为子视图来自定义单元格。     ,        这不是内置控件。它可能是一个自定义样式的UIButton,其.image属性中包含该图像。