【问题标题】:Handling Data Object in UITableViewCell处理 UITableViewCell 中的数据对象
【发布时间】:2015-09-30 21:50:42
【问题描述】:

我想将两行之间的所有内容移到 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

【问题讨论】:

    标签: ios objective-c data-objects


    【解决方案1】:

    在 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,但我有一个习惯是总是将它传递给单元格,因为它通常是一件好事:)

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2010-09-20
        • 2014-12-16
        • 2014-10-04
        • 2011-01-26
        • 1970-01-01
        • 2021-12-27
        • 2015-04-15
        • 2022-09-28
        • 1970-01-01
        相关资源
        最近更新 更多