0

我有一个包含 10 个单元格的 UItableview。我需要将来自 NSarray 的消息显示到这个 tableview 中。数组包含 3 个项目,需要显示在 3 个单元格中的任何一个中。

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

    CustomTableView *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    if (cell == nil) {
        cell = [[ CustomTableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }



    NSString *order = [orderArray objectAtIndex:indexPath.row];

    NSString *sdate = [dateArray objectAtIndex:indexPath.row];


//Label positions    
    UILabel *ordernum = [[UILabel alloc] initWithFrame:CGRectMake(5,28,95,21)];
    UILabel *date = [[UILabel alloc] initWithFrame:CGRectMake(100,28,80,21)];
    UILabel *orderStatusMessage = [[UILabel alloc] initWithFrame:CGRectMake(200,28,80,21)];

    NSString *ordertypeName  =   [ordertypeArray objectAtIndex:indexPath.row];


    ordernum.textColor = [UIColor blackColor];

    [cell.contentView addSubview:ordernum];


    date.textColor = [UIColor blackColor];

     [cell.contentView addSubview:date];



    orderStatusMessage.textColor = [UIColor blackColor];

    if ([ordertypeName isEqualToString:@"saved"]) {
        ordernum.text   =   [NSString stringWithString:@"Saved Order"];



        date.text = [NSString stringWithFormat:@"%@",sdate];




    }
    else{



        if (![order isEqualToString:@""]) {
            for (int i=0; i<[reversed count]; i++)
                {
            stat=[reversed objectAtIndex:i];
                }


        }




    ordernum.text = [NSString stringWithFormat:@"%@",order];

    date.text = [NSString stringWithFormat:@"%@",sdate];



    orderStatusMessage.text =[NSString stringWithFormat:@"%@",stat];

    [cell.contentView addSubview:orderStatusMessage];



    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    cell.selectionStyle = UITableViewCellSelectionStyleGray;


//   } 

    return cell;
}

使用上面的代码,我只能从反向数组中获取最后一项。

请帮忙

4

1 回答 1

1

首先,你的 for 循环是假的

if (![order isEqualToString:@""]) {
        for (int i=0; i<[reversed count]; i++)
            {
        stat=[reversed objectAtIndex:i];
            }


}

为什么每次迭代都循环并覆盖对 stat 的引用?这显然不是你的意图。

其次,我建议重构,因为这段代码非常令人困惑,而且几乎不可能理解其意图。请在重命名变量并稍微清理代码后重新发布。

于 2012-04-12T14:00:07.003 回答