处理 UITableViewCell 中的数据对象

Handling Data Object in UITableViewCell

我想将两行之间的所有内容移到 detailCell.m 中,这样 cellForRowAtIndexPath 中的唯一代码就是剩下的三行。最有效的方法是什么?谢谢你。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCell" forIndexPath:indexPath];
    Job *aJob = self.jobs[indexPath.row];

 _______________
    [cell.titleLabel setText:aJob.title];
    [cell.companyLabel setText:aJob.company];

    if (aJob.logo != (NSString *)[NSNull null]) {
        [cell.logoImageView setImageWithURL:[NSURL URLWithString:aJob.logo] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
    }
    else {
        [cell.logoImageView setImage:[UIImage imageNamed:@"placeholder.jpg"]];
    }
_________________
    return cell;
}

这里是DetailCell.h。 DetailCell.m 目前为空。

@interface DetailCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *companyLabel;
@property (weak, nonatomic) IBOutlet UIImageView *logoImageView;
@end

DetailCell.h

@class Job;
@interface DetailCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *companyLabel;
@property (weak, nonatomic) IBOutlet UIImageView *logoImageView;
-(void) setJobDetail:(Job*) aJob;
@end

在 DetailCell.m 中添加以下方法并导入 Job.h。

-(void) setJobDetail:(Job*) aJob{
    [self.titleLabel setText:aJob.title];
    [self.companyLabel setText:aJob.company];

    if (aJob.logo != (NSString *)[NSNull null]) {
        [self.logoImageView setImageWithURL:[NSURL URLWithString:aJob.logo] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
    }
    else {
        [self.logoImageView setImage:[UIImage imageNamed:@"placeholder.jpg"]];
    }
}

之后您的 cellForRowAtIndex 变为。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCell" forIndexPath:indexPath];
    Job *aJob = self.jobs[indexPath.row];

    [cell setJobDetail:aJob];

    return cell;
}

在 DetailCell.h 中创建一个方法,例如:

- (void)configureCell:(Job *)aJob atIndexPath:(NSIndexPath *)indexPath;

并根据需要在 DetailCell.m 中实现它:

- (void)configureCell:(Job *)aJob atIndexPath:(NSIndexPath *)indexPath {
  [self.titleLabel setText:aJob.title];
  [self.companyLabel setText:aJob.company];

  if (aJob.logo != (NSString *)[NSNull null]) {
    [self.logoImageView setImageWithURL:[NSURL URLWithString:aJob.logo] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
  } else {
    [self.logoImageView setImage:[UIImage imageNamed:@"placeholder.jpg"]];
  }
}

就是这样......当你这样做时,你需要在你的 cellForRowAtIndexPath 方法中做的是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCell" forIndexPath:indexPath];
  Job *aJob = self.jobs[indexPath.row];
  [cell configureCell:aJob atIndexPath: indexPath];
  return cell;
}

你实际上不需要在那个单元格方法中使用 indexPatin,但我有一个习惯,总是将它传递给单元格,因为它通常是一件好事:)