【问题标题】:Prison Cells? Customizing IOS Table View Cells牢房?自定义 IOS 表视图单元格
【发布时间】:2012-11-13 13:53:03
【问题描述】:

我有以下代码

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    UIImage * i1 = [UIImage imageNamed: @"inc_01.png"];
    UIImage * i2 = [UIImage imageNamed: @"inc_02.png"];
    UIImage * i3 = [UIImage imageNamed: @"inc_04.png"];
    UIImage * i5 = [UIImage imageNamed: @"inc_05.png"];
    UIImage * i6 = [UIImage imageNamed: @"inc_06.png"];
    UIImage * i7 = [UIImage imageNamed: @"inchd.png"];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

    if(indexPath.row == 0)
    {
        UIImageView * header= [[UIImageView alloc] initWithImage: i1];

        cell.backgroundView = header;
        // Configure the cell…
    }
    else if (indexPath.row == 2)
    {

        UIImageView *backgroundCellImage=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 11)];

        backgroundCellImage.image=[UIImage imageNamed:@"inc_06.png"];

        [cell.contentView addSubview:backgroundCellImage];
    }
    else
    {
        // Configure the cell…
        UIImageView *imageView = [[UIImageView alloc] initWithImage: i3];

        cell.textLabel.text = @"text";

        UIImageView *backgroundCellImage=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 57, 46)];

        backgroundCellImage.image=[UIImage imageNamed:@"inc_02.png"];

        UIImageView *backgroundCellImage2=[[UIImageView alloc] initWithFrame:CGRectMake(223, 0, 57, 46)];
        backgroundCellImage2.image=[UIImage imageNamed:@"inc_04.png"];

        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(57, 0, 166, 46)];

        label.text = @"wow";

        [cell.contentView addSubview:backgroundCellImage];
        [cell.contentView addSubview:backgroundCellImage2];
        [cell.contentView addSubview:label];


    }

    return cell;  
}

这基本上创建了一个表格视图并将图像放在每个单元格的左侧和右侧。我想要它,以便人们可以单击每个单元格中的左侧或右侧图像,并且根据单元格编号会发生不同的事情。

因此,如果他们单击第 1 行中单元格的左侧图像,则会使用他们单击的行号调用一个函数,并显示一个指示器告诉我他们单击的是左侧图像而不是右侧图像。

如何使用 Objective-c 做到这一点?

【问题讨论】:

  • 你可以有按钮,而不是图像视图。

标签: objective-c ios cocoa-touch ios5 ios4


【解决方案1】:

按如下方式制作自定义原型单元

 ---------------------------------------
| Button 1 |     Text       | Button 2 |
 ---------------------------------------

为两个按钮调用不同的方法。


或者,您可以查看this official documentation,其中详细解释了单元格

将右图添加为accessory view,将左图添加为editing control

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多