0

正如标题所说,我想在我的单元格中有 3 个标签(在 tableView 中)。从下面的代码中可以看出,我目前只有 2 个标签,它们是名称,作为 textLabel 和 book 作为 detailTextLabel。但是,如果我也想将章节作为标签(在 tabelView 单元格中的自己的行)怎么办?实现这一点的最佳方法是什么?

tableView 中的输出应如下所示:

姓名

章节

/谢谢!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"BookmarkCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Bookmark *item = [self.items objectAtIndex:indexPath.row];

NSArray *chunks = [item.name componentsSeparatedByString: @","];

NSString *name;
NSString *book;
NSString *chapter;

if ([chunks count] > 0)
{
    name = [chunks objectAtIndex:0];
    if ([chunks count] > 1)
    {
        book = [chunks objectAtIndex:1];
        if ([chunks count] > 2)
        {
            chapter = [chunks objectAtIndex:2];
        }
    }
}

cell.textLabel.text = name;
cell.detailTextLabel.text = book;
4

3 回答 3

1

听起来你想要一个定制UITableViewCell,然后你可以添加任何你想要的东西。从那里只需命名您放入其中的标签并相应地编写代码以使用cellForRowAtIndexPath方法填充它们。

于 2012-08-27T18:47:10.240 回答
0

创建一个自定义 UIView 并将其作为子视图添加到单元格的 contentView。在这个自定义视图中添加任意数量的 UILalel。您实际上不必创建自定义视图来执行此操作,但它允许更大的多功能性。

这是实现三个标签的一些基本代码。

UIView * pNewContentView= [[UIView alloc] initWithFrame:cell.contentView.bounds];
CGRect labelFrame= pNewContentView.bounds;
labelFrame.size.width= labelFrame.size.width * 0.33;

UILabel* pLabel1=[[UILabel alloc] initWithFrame:labelFrame];
[pNewContentView addSubview:pLabel1];

labelFrame.origin.x= labelFrame.size.width;
UILabel* pLabel2=[[UILabel alloc] initWithFrame:labelFrame];
[pNewContentView addSubview:pLabel2];

labelFrame.origin.x= labelFrame.origin.x + labelFrame.size.width;
UILabel* pLabel3=[[UILabel alloc] initWithFrame:labelFrame];
[pNewContentView addSubview:pLabel3];

[cell.contentView addSubview:pNewContentView];
于 2012-08-27T18:16:35.847 回答
0

如果您需要的只是另一个标签,那么您可以按照标记所说的做,创建一个子视图并将其设置到您的单元格。

如果你想拥有可以做更多的事情,你需要的是一个自定义的 UITableViewCell。您可以定义按钮和其他控件。看看这个苹果提供的文件。这个文档真的可以帮助理解 UITableViewCell 是如何工作的,所以值得一读。

于 2012-08-27T18:24:06.593 回答