如何更改Custom Cell的UILabel文本?

时间:2015-05-16 05:41:42

标签: ios objective-c uitableview uiview uilabel

我有一个带有自定义原型单元格的表视图,单元格上有3个不同的标签。如何访问这些标签?我需要改变他们的文本。我到处搜索过,在这种情况下找不到能帮助我的东西。我有以下代码:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"basicCell" forIndexPath:indexPath];

    //change cell's 3 labels' text here

    return cell;


}

如何访问上面单元格的3个不同标签?

2 个答案:

答案 0 :(得分:5)

您需要创建一个UITableViewCell的子类,并将自定义单元格的类设置为您创建的类。然后,您希望使用单元格中的IBOutlet s将标签链接到您创建的子类。

最后,在-tableView:cellForRowAtIndexPath:中,您应该将单元格转换为新的子类,以便您可以访问这些IBOutlet。假设您的商店名为someLabelOutlet1someLabelOutlet2someLabelOutlet3,请在完成子类化后执行以下

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

    SomeTableViewCellSubclass *cell = (SomeTableViewCellSubclass *)[tableView dequeueReusableCellWithIdentifier:@"basicCell" forIndexPath:indexPath];

    cell.someLabelOutlet1.text = @"sometext"
    cell.someLabelOutlet2.text = @"sometext"
    cell.someLabelOutlet3.text = @"sometext"

    return cell;
}

答案 1 :(得分:0)

cellForRowAtIndexPath中,您可以访问这些标签

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    CustomCell *cell =[tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
    if (!cell) {
    [tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"CustomCell"];
    cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
    // set common properties here
    cell.lbl1.textColor=[UIColor whiteColor];
    cell.lbl1.backgroundColor=[UIColor redColor];
    cell.lbl1.font=[UIFont fontWithName:@"Verdana" size:16.0];
    }
    cell.lbl1.text=@"lbl1Txt";
    cell.lbl2.text=@"lbl2Txt";
    cell.lbl3.text=@"lbl3Txt";
    return cell;
}