【问题标题】:display two values on same cell one on right and 2nd left side of the cell在同一单元格上显示两个值一个在单元格的右侧和第二个左侧
【发布时间】:2011-01-31 13:35:55
【问题描述】:

我想显示两个值(一个是字符串,另一个是整数)。
喜欢以下
string1 00000 int1
string2 00000 int2
string3 00000 int3

0--> 空格

我知道如何在同一个单元格上显示 2 个值,
cell.textLabel.text = [NSString stringWithFormat:@"%@ -- %d",[splitArrayValue objectAtIndex:row],IntegerValue];

但它的显示类似于以下
string1 00 int1
string2 00000 int2
string3 000 int3
没有正确对齐

我想在同一行的第二列显示这个整数值 有可能吗?

提前谢谢你

【问题讨论】:

    标签: iphone uitableview uitabbarcontroller nsmutablearray


    【解决方案1】:

    您应该在单元格中添加两个单独的UILabels,通过它们的标签来区分它们。

    // tableView:cellForRowAtIndexPath
    // ...
    if (cell == nil) {
        cell = [[[UITableViewCEll alloc] init] autorelease];
    
        CGRect leftF = CGRectMake(10, 5, 100, 30);
        CGRect rightF = CGRectMake(120, 5, 100, 30);
    
        UILabel * left = [[[UILabel alloc] initWithFrame:leftF] autorelease];
        left.tag = kLeftLabel; // assumming #define kLeftLabel 100 
    
        [cell.contentView addSubview:left];
    
        UILabel * right = [[[UILabel alloc] initWithFrame:rightF] autorelease];
        right.tag = kRightLabel; // assumming #define kRightLabel 101 
    
        [cell.contentView addSubview:right];
    }
    
    UILabel * leftLabel = (UILabel*)[cell.contentView viewWIthTag:kLeftLabel];
    UILabel * rightLabel = (UILabel*)[cell.contentView viewWIthTag:kRightLabel];
    
    // now put your two values in these two distinct labels
    

    【讨论】:

    • left.tag = kLeftLabel; right.tag = kRightLabel;这有什么需要??
    • 这样您就可以稍后致电viewWithTag: 并访问标签(向下几行)
    【解决方案2】:

    您也可以使用下面的代码。希望对您有所帮助。

    假设 2 个可变数组 - array1 和数组 2。

    在 viewDidLoad 中,分配数组并将值存储在两个数组中。

    (void)viewDidLoad
    {
    
        array1=[NsMutableArray alloc]init];
        [array1 addObject:@"string1"];
        [array1 addObject:@"string2"];
        [array1 addObject:@"string3"];
    
        array2=[NsMutableArray alloc]init];
        [array2 addObject:@"int1"];
        [array2 addObject:@"int2"];
        [array2 addObject:@"int3"];
    }
    

    然后在 cellForRowAtIndexPath 中继续下面的代码。

    (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath      *)indexPath {
    
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        if (cell == nil) {
    
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2         reuseIdentifier:CellIdentifier] ;
        }
    
        cell.textLabel.text =[array1 objectAtIndex:indexPath.row];
    
        cell.detailTextLabel.text =[array2 objectAtIndex:IndexPath.row];
    
        return cell;
    
    }
    

    【讨论】: