创建更详细的表格单元格

时间:2013-05-07 03:45:19

标签: ios objective-c cocoa-touch

我想创建更详细的表格单元格。我找到的一个例子是reddit.com客户端AlienBlue。每个单元格都有一个图像,帖子的标题,然后是其他四个文本。如何在表格单元格中“分层”文本?

4 个答案:

答案 0 :(得分:6)

您好,您应该将UITableViewCell子类化为表格视图中单元格的自定义用户界面。 另请参阅:UITableViewDataSourceUITableViewDelegate了解有关表格视图的其他选项。

你的子类UiTableViewCell可能会有标题(UILabel),标题(UILabel)和图像(UIImageView)的自定义字段

示例:

  1. Crafting Custom UITableView Cells

  2. Customize Table View Cells For UITableView

答案 1 :(得分:2)

您需要创建自定义UITableViewCell。它非常简单,可以完全用代码(我最喜欢的)或通过IB来完成。

Google custom UITableView cell提供了一些很棒的教程。

答案 2 :(得分:1)

  1. 使用 NSMUtableDictionary NSMutableArray ,而不是NSArray或NSMutableArray来保存数据(字符串,图片,...)。
  2. 将表格单元格样式属性设置为 UITableViewCellStyleSubtitle ,以便表格可以显示详细的TextLabel(cell.detailTextLabel.text)。
  3. 您可以将图像设置为表格单元格。
  4. 实施例

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    UIImage *image = [[list1 objectAtIndex:indexPath.row] objectForKey:key1a];
    cell.imageView.image = image;
    NSString *categoryname = [[list1 objectAtIndex:indexPath.row] objectForKey:key1b];
    cell.textLabel.text = categoryname;
    cell.detailTextLabel.text = [[list1 objectAtIndex:indexPath.row] objectForKey:key1e];
    cell.detailTextLabel.textColor = [UIColor blueColor];
    return cell;
    }
    

答案 3 :(得分:0)

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

        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) 
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        cell.textLabel.text=[NSString stringWithFormat:@"%@",[appdelegate.name objectAtIndex:indexPath.row]];

        UIImage *image = [[array1 objectAtIndex:indexPath.row];

        cell.detailTextLabel.text = [[array1 objectAtIndex:indexPath.row];


        return cell;
     }