2011-01-20 5 views

ответ

3

Существует два варианта (может быть больше). Вы можете использовать собственные свойства UITableViewCell для добавления содержимого в ячейку или создать пользовательскую ячейку (я имею в виду, добавьте свои собственные представления в ячейку). Для начала попробуйте первый, он прост и элегантен, и результаты будут весьма хорошими. Например попробуйте следующий метод создания клеток:

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     // notice the Style. The UITableViewCell has a few very good styles that make your cells look very good with little effort 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell... 
    // In my case I get the data from the elements array that has a bunch on dictionaries 
    NSDictionary *d = [elements objectAtIndex:indexPath.row]; 

    // the textLabel is the main label 
    cell.textLabel.text = [d objectForKey:@"title"]; 

    // the detailTextLabel is the subtitle 
    cell.detailTextLabel.text = [d objectForKey:@"date"]; 

    // Set the image on the cell. In this case I load an image from the bundle 
    cell.imageView.image = [UIImage imageNamed:@"fsaint.png"]; 

    return cell; 
} 
0

Я большой поклонник перекрывая класс UITableViewCell, и делать заказ рисования в self.contentView. Этот метод немного сложнее, но это приводит к значительно лучшей производительности прокрутки.

Например, предположим, что вы переопределить ячейку, и имеют 3 свойства на него так:

@property(nonatomic, retain) UIImage *userPic; 
@property(nonatomic, retain) NSString *label; 
@property(nonatomic, retain) NSString *date; 

Затем вы можете нарисовать их в клетке с помощью (DrawRect :) функцию:

- (void)drawRect:(CGRect)rect { 
    [super drawRect:rect]; 
    [userPic drawInRect: CGRectMake(10, 5, 50, 50)]; 
    [label drawAtPoint:CGPointMake(70, 5) withFont:[UIFont boldSystemFontOfSize:17]]; 
    [date drawAtPoint:CGPointMake(70, 30) withFont:[UIFont systemFontOfSize:14]]; 
    } 

Для получения дополнительных примеров попробуйте проверить эту структуру, которая использует этот стиль: https://github.com/andrewzimmer906/XCell

Смежные вопросы