0

在我的 tablview 行中显示成功文本后,我没有设法在行中放置标签和一些额外的文本。作为普通字体的文本和更小字体的额外文本。当我执行此代码时,我的设备会在我的数据库中显示 18 行,其中包含“creation_date”的内容。但我想显示每一行的“creation_date”和“名称”。如果我复制并粘贴 cell.textLabel 行并将“creation_date”更改为“名称”,我会得到一个 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;

}

4

1 回答 1

2

我建议使用自定义 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 的数据源中使用的单元格标识符。

表视图的数据源:

 - (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
 }
于 2013-11-05T17:26:29.283 回答