2012-08-13 144 views
0

您好我有一个要求来定制UITableViewCell。因此,我创建了一个自定义类和必要的UI(xib)来支持它。对于XIB,我选择了该类作为我创建的派生类。我的问题是,当显示标签链接到属性后,我设置运行时的值不显示所需的文本。它的左边是空白的。以下是代码片段。ios TableViewCell自定义问题。不显示自定义文本

@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; 
} 
+0

您正在将'cellForRowAtIndexPath'放入自定义单元类中? – 2012-08-13 18:35:01

回答

0

首先,我希望你cellForRowAtIndexPath在你UITableViewdelegate,而不是在您的自定义单元格类。

其次,现在的问题是:

// 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中创建了标签。

+0

对不起,我正在玩代码,忘了发表评论。出。我只是按照您的建议进行了更改,但仍然无效。如果我尝试使用默认值 cell.textLabel.text = currentCellWeight.display; 但这意味着我无法按照我需要的方式自定义单元格。 – user959671 2012-08-13 20:18:01

+0

确保您的单元格标识符已设置为“MedVaultCell”,并且您单元格的类别设置为“CustomCell”,并且您的IBOutlet已正确连接。 – 2012-08-13 20:23:03

+0

很酷。帮助。谢谢Justin! – user959671 2012-08-13 21:16:21