在自定义(静态)tableviewcell中设置标签的文本

时间:2013-12-30 09:16:18

标签: iphone objective-c uitableview

我创建了一个TableViewController,我在每个单元格中设置了2个标签的自定义单元格。我已通过.h文件中的以下代码将各个单元格链接到.m文件:

@interface NewChatScreen2 : UITableViewController
{
 IBOutlet UITableViewCell *cell1;
 IBOutlet UITableViewCell *cell2;
}
@end

在一个例子中,我用特定单元格中的2个标签中的一个标记了数字80001,现在我尝试通过以下代码更改cell1中该特定标签的文本:

UILabel *cell1NameLabel = (UILabel *)[cell1.contentView viewWithTag: 80001];
cell1NameLabel.text = @"Success namechanged!!"; 

然而,这不起作用。此外,我还意识到,如果我只是将界面设计指向自定义类,则每个tableviewcells中的自定义文本将变为空白。我是iOS开发的新手,希望问我的代码有什么问题,或者我试图更改这些自定义标签的方式。

2 个答案:

答案 0 :(得分:0)

您必须实现UITableViewDelegate和UITableViewDataSource方法,并且在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中,您必须更改单元格数据或外观。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"YourCellIdentifier";

    YourCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"YourCustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    cell.yourLabel.text = @"some text";
}

答案 1 :(得分:0)

看看这个

UILabel *cell1NameLabel = (UILabel *)[cell viewWithTag:101];
cell1NameLabel.text = @"Success namechanged!!"; 
相关问题