繁体   English   中英

在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中创建一个类似于以下内容的方法:

- (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;
}

您实际上不需要那种cell方法中的indexPatin,但是我有一种习惯,总是将其传递到cell中,因为通常可以使用它:)

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM