UITableView реплицирует первый элемент NSArray для всех строк - PullRequest
2 голосов
/ 15 августа 2010

У меня странная проблема с моими NSMutableArray и UITableView.

У него есть 4 элемента в массиве, таблица показывает 4 строки, но каждая строка содержит только первый элемент массива, а не строку 1, показывающую элемент 1 массива, строку 2, показывающую элемент 2 и т. Д.

Вот мой код:

Мой изменяемый массив // в .h

NSMutableArray *tableRows;

// in .m

tableRows = [[NSMutableArray arrayWithObjects:@"For Business, For Pleasure",
                  @"A Great Decision",
                  @"Air Charter Your Honeymoon",
                  @"Time Flies", nil] retain];

Источник данных и делегат в моей таблице

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return (NSInteger)[tableRows count];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"CELL_AIR";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if(cell == nil) {
        cell = [self makeTableCell:cellID identifier:indexPath];
    }
    return cell;
}

-(UITableViewCell *)makeTableCell:(NSString *)identifier identifier:(NSIndexPath *)indexPath {
    //frames
    CGRect cellFrame = CGRectMake(0, 10, 320, 50);
    CGRect lbl1Frame = CGRectMake(10, 0, 320, 25);
    CGRect lbl2Frame = CGRectMake(10, 20, 320, 20);


    UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:cellFrame reuseIdentifier:identifier];

    UILabel *lbl1 = [[UILabel alloc] initWithFrame:lbl1Frame];
    lbl1.tag=1;
    lbl1.font = [UIFont systemFontOfSize:19];
    lbl1.text = [tableRows  objectAtIndex:indexPath.row];


    UILabel *lbl2 = [[UILabel alloc] initWithFrame:lbl2Frame];
    lbl2.tag=2;
    lbl2.text = @"Tap to read more...";
    lbl2.font = [UIFont systemFontOfSize:12];

    [cell.contentView addSubview:lbl1];
    [cell.contentView addSubview:lbl2];

    return cell;
}

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

Любая помощь будет принята с благодарностью!

Спасибо

C

Ответы [ 2 ]

3 голосов
/ 15 августа 2010

Это вызывает ошибку:

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    return 1;
}

У вас есть только 1 строка для каждого раздела. Поэтому для каждого раздела путь индекса снова начинается с 0, после чего вы получаете только первый элемент в массиве

Я думаю, что лучший способ сделать это:

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    return (NSInteger)[tableRows count];
}
1 голос
/ 16 августа 2010

Добавление к тому, что ответил Водханг, вот как я бы это сделал:

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
return [tableRows count];
}

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(cell == nil) {
    cell = [self makeTableCell:cellID identifier:indexPath];
}

[[cell textLabel]
         setText:[tableRows objectAtIndex:indexPath.row]];

return cell;
}

Я вообще не использую (NSInteger) [количество массивов].Поскольку метод count возвращает целочисленный тип.Надеюсь, это поможет.

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