nithin
nithin

Reputation: 2467

Insert NSarray items into specified UItableviewcell

I have a UItableview with 10 cells.I need to display a message from an NSarray into this tableview.The array contains 3 items and needs to be displayed in the any of the 3 cells .

 - (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;
}

With the above code I am getting only the last item from reversed array.

Please help

Upvotes: 0

Views: 184

Answers (1)

Sean Freitag
Sean Freitag

Reputation: 920

To start, your for loop is bogus

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


}

Why are you looping and overwriting the reference to stat with each iteration? That's clearly not your intent.

Second, I would advise refactoring, since this code snippit is extremely confusing and it is very close to impossible to understand the intent. Please repost after renaming variables and cleaning the code a bit.

Upvotes: 1

Related Questions