Динамическое добавление данных в UITableView - PullRequest
0 голосов
/ 14 января 2011

Эй, ребята!Я новичок в Objective C и мне нужна небольшая помощь.Я создал UITableView в UIViewController.Я хотел бы знать, как динамически заполнять мой UITableView.Данные представляют собой набор меток, которые я сохранил в NSMutableArray.Таким образом, каждая строка отображает содержимое массива.После перезагрузки массива со свежими данными вторая строка будет продолжать отображать данные по мере их добавления в массив.Заранее благодарю за любую помощь!

Ответы [ 2 ]

0 голосов
/ 14 января 2011

Просто сделай так ..................

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *celltype=@"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:celltype];


    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.backgroundColor=[UIColor clearColor];
        //cell.textLabel.text=[[resultarray objectAtIndex:indexPath.row] valueForKey:@"Service"];

        cell.backgroundView=[[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cell.png"]]autorelease];
                            // [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back.png"]] autorelease];

        cell.selectionStyle=UITableViewCellSelectionStyleNone;



    }


    UILabel *productCodeLabel = [[UILabel alloc] initWithFrame:CGRectMake(105, 7, 60,20 )];
    productCodeLabel.textColor = [UIColor whiteColor];
    productCodeLabel.backgroundColor=[UIColor clearColor];
    productCodeLabel.text=[NSString stringWithFormat:@"%@",@"Code"];
    [cell.contentView addSubview:productCodeLabel];
    [productCodeLabel release];

    UILabel *CodeValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(170, 7, 140,20 )];
    CodeValueLabel.textColor = [UIColor whiteColor];
    CodeValueLabel.backgroundColor=[UIColor clearColor];
    CodeValueLabel.text=[NSString stringWithFormat:@"%@",[[productArray objectAtIndex:indexPath.row]valueForKey:@"ID"]];
    [cell.contentView addSubview:CodeValueLabel];
    [CodeValueLabel release];



    UILabel *productNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(105, 35, 60,20 )];
    productNameLabel.textColor = [UIColor whiteColor];
    productNameLabel.backgroundColor=[UIColor clearColor];
    productNameLabel.text=[NSString stringWithFormat:@"%@",@"Name"];
    [cell.contentView addSubview:productNameLabel];
    [productNameLabel release];

    UILabel *NameValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(170, 35, 140,20 )];
    NameValueLabel.textColor = [UIColor whiteColor];
    NameValueLabel.backgroundColor=[UIColor clearColor];
    NameValueLabel.text=[NSString stringWithFormat:@"%@",[[productArray objectAtIndex:indexPath.row]valueForKey:@"Title"]];
    [cell.contentView addSubview:NameValueLabel];
    [NameValueLabel release];



    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(105, 68, 60,20 )];
    dateLabel.textColor = [UIColor whiteColor];
    dateLabel.backgroundColor=[UIColor clearColor];
    dateLabel.text=[NSString stringWithFormat:@"%@",@"Date"];
    [cell.contentView addSubview:dateLabel];
    [dateLabel release];

    UILabel *dateValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(170, 68, 140,20 )];
    dateValueLabel.textColor = [UIColor whiteColor];
    dateValueLabel.backgroundColor=[UIColor clearColor];
    dateValueLabel.text=[NSString stringWithFormat:@"%@",[[productArray objectAtIndex:indexPath.row]valueForKey:@"PostedDate"]];
    dateValueLabel.font=[UIFont systemFontOfSize:14];
    dateValueLabel.numberOfLines=3;
    dateValueLabel.adjustsFontSizeToFitWidth=YES;
    [dateValueLabel setLineBreakMode:UILineBreakModeCharacterWrap];
    [cell.contentView addSubview:dateValueLabel];
    [dateValueLabel release];

    /*
     UILabel *DetailLabel = [[UILabel alloc] initWithFrame:CGRectMake(185, 15, 100,20 )];
     DetailLabel.textColor = [UIColor blackColor];
     DetailLabel.backgroundColor=[UIColor clearColor];

     [cell.contentView addSubview:DetailLabel];
     [DetailLabel release];*/
    return cell;

}

У меня есть массив с ключами, поэтому я также использую ключ. Но если у вас просто есть массив без ключей. Тогда вам не нужно писать ValuForKey: @ "Id", вы можете пропустить значение для ключа. Вам поможет, я дал вам полный код.

0 голосов
/ 14 января 2011

@ tableView, так как вы хотите, чтобы конкретная строка, т.е. вторая строка отображала данные после перезагрузки массива (т.е. предположим, что myArray), для этого я предлагаю вам сделать еще один массив типа NSMutableArray, предположив, что это copymyArray.... и после перезагрузки myArray передайте объекты myArray в copymyArray.Теперь во второй строке индекс строки дают copymyArray ........ а по остальному индексу tableview дают myArray.Надеюсь, это поможет тебе!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...