【问题标题】:Text format in a row in Objective cObjective c中一行中的文本格式
【发布时间】:2013-11-05 17:14:40
【问题描述】:

在我的 tablview 行中显示成功文本后,我没有设法在行中放置标签和一些额外的文本。作为普通字体的文本和更小字体的额外文本。 当我执行此代码时,我的设备会在我的数据库中显示 18 行,其中包含“creation_date”的内容。但我想显示每一行的“creation_date”和“名称”。如果我复制并粘贴 cell.textLabel 行并将“creation_date”更改为“name”,我会得到一个 SIGABRT:8 如果有人可以帮助我吗?这是我的代码:

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

static NSString *MyIdentifier = @"Cell";
NSString *LBLS = @"go";
NSString *LBLR =@"smogogo";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];

CGRect frame = CGRectMake(0, 0, 160, 50);
UILabel *lbl1 = [[UILabel alloc] initWithFrame:frame];
lbl1.textAlignment = UITextAlignmentRight;
[lbl1 setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];
[lbl1 setTextColor:[UIColor grayColor]];
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
LBLS = [[stories objectAtIndex:storyIndex] objectForKey: @"module"];
if ([LBLS isEqualToString:dataType]){
lbl1.text = [[stories objectAtIndex: storyIndex] objectForKey: @"creation_date"];
LBLS = [[stories objectAtIndex:storyIndex] objectForKey: @"module"];
if ([LBLS isEqualToString:dataType])
[cell.contentView addSubview:lbl1];
    [lbl1 release];}

CGRect frame2 = CGRectMake(0, 0, 250, 50);
UILabel *lbl2 = [[UILabel alloc] initWithFrame:frame2];
lbl2.textAlignment = UITextAlignmentRight;
[lbl2 setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];
[lbl2 setTextColor:[UIColor blackColor]];
lbl2.text = [[stories objectAtIndex: storyIndex] objectForKey: @"name"];
if ([LBLS isEqualToString:dataType]){
    [cell.contentView addSubview:lbl2];
    [lbl2 release];}
return cell;

}

【问题讨论】:

  • 自 iOS 3.0 起,UITableViewCell initWithFrame:reuseIdentifier: 方法已被弃用。使用正确的方法设置单元格的样式。
  • 好的,我会想办法重新编辑,但我担心的是 cell.textLabel 行。
  • 您有什么顾虑?你的问题很不清楚。
  • 谢谢你不放弃^^。当我执行此代码时,我的设备会显示 18 行以及我的数据库中的 creation_date。但我想显示 creation_date 和每一行的名称。如果我复制并粘贴 cell.textLabel 行并使用名称更改 creation_date,我会得到一个 SIGABRT :8。
  • 用给你带来问题的代码更新你的问题。

标签: ios objective-c text


【解决方案1】:

我建议使用自定义 UITableViewCell:

LaFiltersTableCell.h

@interface LAFiltersTableCell : UITableViewCell {
}
@property (strong, nonatomic) IBOutlet UILabel *filterNameLabel;
@property (strong, nonatomic) IBOutlet UILabel *selectedOptionsLabel;

LaFiltersTableCell.m

 + (LAFiltersTableCell *)getNewLAFiltersTableCell {
     NSArray *xib = [[NSBundle mainBundle] loadNibNamed:@"LAFiltersTableCell" owner:nil options:nil];
     for (NSObject *obj in xib) {
         if ([obj isKindOfClass:[LAFiltersTableCell class]]) {
             return (LAFiltersTableCell *)obj;
         }
     }
     return nil;

}

为您的自定义 Cell 制作一个 nib 文件并记住以下几点:nib 视图的类必须是 LAFiltersTableCell,链接您要设置的 IBOutlets,设置您将在 tableview 的数据源中使用的单元格标识符.

tableview 的数据源:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     LAFiltersTableCell *filterCell = [tableView dequeueReusableCellWithIdentifier:@"LAFiltersTableCell"];
     if (filterCell == nil) {
         filterCell = [LAFiltersTableCell getNewLAFiltersTableCell];
     }
     filterCell.filterNameLabel = @"Test";
     filterCell.selectedOptionsLabel = @"Some other Test";
    return filterCell
 }

【讨论】:

  • 我会深入了解您的答案,并告诉我是否设法实现了这一目标。谢谢你的回答。
  • 完全没问题。了解如何处理自定义表格单元格非常有用。这将提供一个良好的实践示例;请告诉我反馈
  • 当然,:D 通常 SIGABRT 信号是当您忘记将 xib 文件添加到项目时。你创建了 LAFiltersTableCell.xib 吗?
  • :D 谢谢,我指出一个小问题 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];单元格 = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];这里的问题是,您将一个单元格出列(意味着您正在重用一个已经创建的单元格),然后您正在分配另一个单元格;这里的问题是,如果您有一个包含 200 个对象的数据源,您将分配 200 个单元格,这是我们不想要的。
  • 完全没问题 :D 另外我建议不要在 cellForRowAtIndexPath 中分配东西,比如 UILabel *lbl2 = [[UILabel alloc] initWithFrame:frame2];想象一下,您正在制作包含价格、特价、品牌、产品名称和图片的服装目录。如果您花时间在每次滚动时分配东西,也许在 iPod 4 上性能不会是最好的:D。当然,对于 2 个额外的标签,差异很小,但是如果您要向其中添加一个带有加载微调器的图像,您会看到应用程序运行缓慢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 2019-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多