ios - 如何在自己的行上制作每个customLabel?

标签 ios objective-c listview uiview uilabel

我试图为listView中的书签功能创建一个带有3个标签,名称,书籍和章节的自定义UIView。现在,我希望标签在tableView的单元格中位于不同的行中。最好的方法是什么?或者,如果您看下面的代码,您将如何做自己的标签?我是Objective C和Iphone开发的新手。

结果应该在每个单元格的列表中看起来像这样:

--------
name

book

chapter
--------
- (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];
        }
    }
}

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

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];

[pLabel1 setText:(name)];    
[pLabel2 setText:(book)];  
[pLabel3 setText:(chapter)];       

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

return cell;
}

最佳答案

将所有x替换为y,将width替换为height。检查输出并调整标签的高度和宽度以满足您的要求。

关于ios - 如何在自己的行上制作每个customLabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12158695/

相关文章:

ios - 快速实现导航菜单的标准方法是什么?

ios - 我应该在哪里放置自动布局代码?

ios - 输入 applicationDidBecomeActive 时本地通知不起作用

objective-c - iOS Objective-C 访问来自不同类的 ivars

c# - 为什么我的 ListView 不显示列表或详细信息项目?

Android如何检测setOnItemClickListener中的第二次点击

ios - 安装 iPhone 配置实用程序时出错

ios - IOS Safari 上的实习生 3.4.1 "unable to set accept insecure certs on Safari"

android - 带类映射器 : No setter/field for "class" found on class in android

ios - ({}) 在objective-c中可以包装代码并返回一个对象,它是 block 还是别的?