为每个单元格添加额外的UIlabel

Lea*_*nne 1 objective-c uitableview uilabel ios

我正在尝试在UILabel每个单元格中添加一个额外的(UITableView)我在
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中成功使用了这个代码

这是我的代码

//add another text
UILabel *test = [[UILabel alloc] initWithFrame:CGRectMake(250,80,50,20.0)];
test.text =  [NSString stringWithFormat: @"test"];
test.backgroundColor = [UIColor clearColor];
test.font=[UIFont fontWithName:@"AppleGothic" size:20];
[cell addSubview:test];
Run Code Online (Sandbox Code Playgroud)

但是我想如果我这样做,我不能为每个单元格添加不同的文本,而是在所有单元格中以相同的文本结束.谁能告诉我怎么做?
哦,另一个问题是,如果我这样做,除了第一个以外,每个单元格都会显示"test".

Ara*_*han 5

看一下教程" UITableView - 将子视图添加到单元格的内容视图 ".

尝试这样的事情

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

    CGRect CellFrame = CGRectMake(0, 0, 320, 65);
    CGRect Label1Frame = CGRectMake(17,5,250,18);  

    UILabel *lblTemp;

    UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];  
    lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
    [lblTemp setFont:[UIFont fontWithName:@"Arial-BoldMT" size:15]];
    lblTemp.tag = 1;
    lblTemp.backgroundColor=[UIColor clearColor];
    lblTemp.numberOfLines=0;
    [cell.contentView addSubview:lblTemp];      
    return cell;  // this I forgot  
}
Run Code Online (Sandbox Code Playgroud)

在cellForRowAtIndexPath中

{
    if(cell == nil)
            cell = [self getCellContentView:CellIdentifier];

    UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];  
    lblTemp1.text =[nameArray objectAtIndex:value+indexPath.row];  
        return cell;
}
Run Code Online (Sandbox Code Playgroud)