0

Hi I have an requirement to customize UITableViewCell. Hence I have created a custom class and necessary UI (xib) to support it. For the XIB I have chosen the class as the derived class that I have created. My Problem is when after linking the display labels to the properties and me setting the values at runtime does not display the text desired. Its left as blank. Below is the code snippet.

@interface CustomCell : UITableViewCell
{
    IBOutlet UILabel *titleRow;
}

@property (nonatomic, strong) UILabel *titleRow;
@property (nonatomic, strong) UILabel *subTitleRow;
@property (nonatomic, strong) UILabel *otherTextRow;
@end

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

    // Configure the cell...
    if (nil == cell){

        //Load custom cell from NIB file
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellHistoryCell" owner:self options:NULL];
        cell = [nib objectAtIndex:0];

        //cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        //cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    }   

    // get the object
    Weight *currentCellWeight = [_weights objectAtIndex:indexPath.row];

    // Configure the cell...
    UILabel *titleLable = [[UILabel alloc]init];
    titleLable.text = currentCellWeight.customDispText;
    [cell setTitleRow:titleLable];

    cell.titleRow.text = currentCellWeight.display;
    cell.titleRow.textColor = [UIColor redColor];
    //cell.textLabel.text = [[_weights objectAtIndex:indexPath.row] customDispText];
    //cell.textLabel.textColor = [UIColor whiteColor];


    return cell;
}
4

1 回答 1

0

首先,我希望你cellForRowAtIndexPath是在你的UITableView delegate,而不是在你的自定义单元格中。

其次,问题来了:

// Configure the cell...
UILabel *titleLable = [[UILabel alloc]init];
titleLable.text = currentCellWeight.customDispText;
[cell setTitleRow:titleLable];

在此代码中,您将创建一个新标签并用新标签覆盖您的 IBOutlet 标签。那么您不会显示新标签。相反,将代码更改为:

// Configure the cell...
cell.titleRow.text = currentCellWeight.customDispText;

但是,您随后将其重置titleRow.textcurrentCellWeight.display.

因此,您需要选择其中哪一个作为文本并将文本设置为该文本。您不需要创建新标签 ( UILabel *titleLable = [[UILabel alloc] init];),因为您已经在 IB 中创建了标签。

于 2012-08-13T18:39:56.530 回答