Представление таблицы с пользовательской ячейкой (программно) - PullRequest
6 голосов
/ 27 марта 2010

До сих пор я использовал для создания пользовательских перьев, чтобы сделать свою ячейку нужной, но на этот раз высота ячейки будет изменяться от одной к другой, поэтому я не могу создать перо ячейки фиксированного размера. *

Итак, я решил создать его программно ... Способ ниже - хороший способ достичь этого?

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UILabel *pseudoAndDate = [[UILabel alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,20.0)];
        [pseudoAndDate setTag:1];
        [cell addSubview:pseudoAndDate];
        [pseudoAndDate release];
    }

    CommentRecord *thisRecord = [comments objectAtIndex:[indexPath row]];

    UILabel *label = (UILabel *)[cell viewWithTag:1];
    [label setText:[NSString stringWithFormat:@"%@ | %@",thisRecord.author,thisRecord.date]];

    return cell;
}

или .. я что-то здесь упускаю? Потому что пока это не работает;)

Спасибо,

Готье.

Ответы [ 3 ]

0 голосов
/ 11 ноября 2013

Если ваша проблема в том, что высота меняется от ячейки к ячейке, вы можете использовать метод:

https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:

От UITableViewDelagate для достижения этого

0 голосов
/ 18 сентября 2015

Новая ссылка для пользовательского UITableViewCell программно Документация Apple UITableViewCell

0 голосов
/ 11 августа 2013

Зачем создавать ярлык, когда вам это не нужно? Используйте метку UITableViewCell.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    CommentRecord *thisRecord = [comments objectAtIndex:[indexPath row]];

    cell.textLabel.text = [NSString stringWithFormat:@"%@ | %@",thisRecord.author,thisRecord.date];

    return cell;
}
...