如何在iphone的tableview中的每一行添加标签

时间:2012-07-02 05:27:17

标签: iphone tableview

我想以编程方式在iphone行的右侧的tableview中的每一行添加标签。 任何人都可以帮助我吗?

4 个答案:

答案 0 :(得分:1)

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
UILabel* lbl = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
    lbl.font = [UIFont boldSystemFontOfSize:17.0];
    lbl.text = [self.arrRows objectAtIndex:indexPath.row];
    [cell.contentView addSubview:lbl];
    [lbl release];
return cell;

答案 1 :(得分:0)

你可以这样做:

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    UILabel *lblText = [[UILabel alloc] initWithFrame:CGRectMake(200, 3, 100, 15)]; // For right alignment
    lblText.text = [self.arrText objectAtIndex:indexPath.row];
    lblText.textColor = [UIColor blackColor];
    [cell addSubview:lblText];
    [lblText release];

    return cell;
}

答案 2 :(得分:0)

我认为你要求代码。  尝试

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

static NSString *CellIdentifier = @"Cell";
UILabel *yourLabel = nil;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    yourLabel = [[UILabel alloc] initWithFrame:CGRect……];
    [yourLabel setTag:9999];
    [[cell contentView] addSubview:addressLabel];

} 

if (!yourLabel)
    yourLabel = (UILabel*)[cell viewWithTag:9999];

return cell;

}

答案 3 :(得分:0)

cellForRowAtIndexPath:方法中: -

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier=@"Cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        //set reuseIdentifier:nil for avoid label overlapping
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    }

    //make your label as according to you
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
    label.backgroundColor = [UIColor redColor];

    // add label content view of cell
    [cell.contentView addSubview:label];

    return cell;
}