Рисование пользовательских UITableCells с правильными данными - PullRequest
0 голосов
/ 04 марта 2010

У меня есть UITableView с пользовательскими табличными ячейками. Сначала у меня была версия, в которой реализован отдельный файл NIB для создания пользовательского представления ячейки, но это делает прокрутку ужасно медленной. Официальный ответ - сделать свой собственный чертеж, поэтому я реализовал вариант чертежа ( ABTableViewCell ).

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

Мой RootViewController реализует UITableViewDataSource, и там инициализируется CustomCell.

Я убедился, что все данные правильно передаются в CustomCell, поэтому он должен иметь отношение к чертежу. Я думаю, что это связано с тем фактом, что метод drawContentView вызывается только для ячеек, которые находятся в поле зрения, а не для тех, которые прокручиваются в поле зрения.

Как мне снова вызвать drawContentView, чтобы обновить появившиеся ячейки? Это то, что я должен сделать из моего RootViewController, используя что-то еще, чем: (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath?

CustomCell.h:

#import "ABTableViewCell.h"

@interface CustomCell : ABTableViewCell {
  NSString * cellSubText;
  NSString * cellText;
  NSString * cellImage;
  NSString * cellIcon;
  NSString * cellId;
}

@property (nonatomic, copy) NSString *cellSubText;
@property (nonatomic, copy) NSString *cellText;
@property (nonatomic, copy) NSString *cellImage;
@property (nonatomic, copy) NSString *cellIcon;
@property (nonatomic, copy) NSString *cellId;

@end

CustomCell.m:

#import "CustomCell.h"

@implementation CustomCell

@synthesize cellSubText, cellText, cellImage, cellIcon, cellId;

static UIFont *celSubFont = nil;
static UIFont *cellFont = nil;

+ (void)initialize {
    if(self == [CustomCell class]) {
        cellSubFont = [[UIFont systemFontOfSize:13] retain];
        cellFont = [[UIFont boldSystemFontOfSize:17] retain];
    }
}

- (void)setFirstText:(NSString *)s {
    [cellSubText release];
    cellSubText = [s copy];
    [self setNeedsDisplay]; 
}

- (void)setLastText:(NSString *)s {
    [cellText release];
    cellText = [s copy];
    [self setNeedsDisplay]; 
}

- (void)drawContentView:(CGRect)r {
    CGContextRef context = UIGraphicsGetCurrentContext();

    UIColor *backgroundColour = [UIColor whiteColor];
    UIColor *categoryColour = [UIColor grayColor];
    UIColor *titleColour = [UIColor blackColor];
  if(self.highlighted || self.selected) {
    backgroundColour = [UIColor clearColor];
        categoryColour = [UIColor whiteColor];
    titleColour = [UIColor whiteColor];
    }

    [backgroundColour set];
    CGContextFillRect(context, r);

    CGPoint pCategory;
    pCategory.x = 60;
    pCategory.y = 3;

    [categoryColour set];
  [cellSubText drawAtPoint:pCategory forWidth:235 withFont:categoryFont minFontSize:13 actualFontSize:NULL lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentNone];  

  CGPoint pTitle;
    pTitle.x = 60;
    pTitle.y = 17;

  [titleColour set];
  [cellText drawAtPoint:pTitle forWidth:235 withFont:titleFont minFontSize:17 actualFontSize:NULL lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentNone];  

  //Display the image
  CGPoint pImage;
    pImage.x = 5;
    pImage.y = 5;

  NSData *imageData = [[NSData dataWithContentsOfURL:[NSURL URLWithString:cellImage]] retain];
  UIImage *image = [UIImage imageWithData:imageData];
  [image drawAtPoint:pImage];
}

- (void)dealloc {
  [cellSubText release];
  [cellText release];
  [cellIcon release];
  [cellImage release];
  [super dealloc];
}
@end

RootViewController.m

#import "CustomCell.h"
#import "MyAppDelegate.h"
#import "RootViewController.h"
#import "DetailViewController.h"

@implementation RootViewController
@synthesize tableDataArray;

- (void)viewDidLoad {
  [super viewDidLoad];
}

//Override the default initWithNibName method
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil tableDataSource:(NSArray*)tableData {
  if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    // Custom initialization
    tableDataArray = [tableData retain];   
  }
  return self;
}

-(void)viewWillAppear:(BOOL)animated {
  appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];    
  [super viewWillAppear:animated];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return [self.tableDataArray count];
}

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

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if(cell == nil) {
        cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CustomCellIdentifier] retain];
    }
  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  NSUInteger row = [indexPath row];
  NSDictionary *rowData = [self.tableDataArray objectAtIndex:row];
    cell.cellSubText = [rowData objectForKey:@"Date"];
    cell.cellText = [rowData objectForKey:@"Name"];
  cell.cellImage = [rowData objectForKey:@"Image"];
  cell.cellId = [rowData objectForKey:@"Id"];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
  NSString *selectedCell = [tableDataArray objectAtIndex:indexPath.row];

  DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
  detailViewController.selectedCell = selectedCell;
  [self.navigationController pushViewController:detailViewController animated:YES];
  [detailViewController release];
  detailViewController = nil;
}

- (void)dealloc { 
    [super dealloc];
}

@end

Ответы [ 3 ]

2 голосов
/ 05 марта 2010
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if(cell == nil) {
        cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CustomCellIdentifier] retain];
    }

статическая NSString * CustomCellIdentifier = @ "CustomCellIdentifier";

Вышеуказанный жирный код делает такое поведение за вас.

В iphone tableview повторно использует уже созданные ячейки. которые не видны. Пример: первые 8 ячеек будут созданы отдельно. И когда вы прокрутите вниз, первые несколько строк будут скрыты. Теперь скрытые ячейки будут повторно использоваться ячейками, которые будут показаны сейчас. Вот почему вы получаете такое поведение :) Попробуйте дать уникальный идентификатор, если содержимое вашего табличного представления меньше.

или

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

- (недействительно) prepareForReuse

, который вызывается непосредственно перед возвратом ячейки из

- (UITableViewCell *) dequeueReusableCellWithIdentifier: (NSString *) идентификатор

1 голос
/ 07 апреля 2014
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int myButtonTag = 1000+(10*indexPath.section)+indexPath.row;
    NSString *kCellID = [NSString stringWithFormat:@"cellId_%i",myButtonTag];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
    if (cell == nil)
    {
       //[[cell viewWithTag:myButtonTag] removeFromSuperview];
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
    }

    cell.accessoryType = UITableViewCellAccessoryNone;
}

Чтобы удалить данные из вашей ячейки во время быстрой прокрутки в UITableView (особенно в среде, управляемой ARC), не забудьте установить уникальный идентификатор CellIdentifier в строке cellId, который удаляет устаревшую ячейку. Таким образом, это освобождает его от заполнения повторяющихся клеток. Надеюсь, вы найдете это подходящим и по своему вкусу. Ура! * * 1002

1 голос
/ 04 марта 2010

Возможно, табличное представление повторно использует ваши ячейки и дает вам случайные результаты, повторно используя ячейки, настроенные в настоящее время; Вы смотрели в - (void)prepareForReuse на UITableViewCell классе?

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