0

cellForRowAtIndexPath の唯一のコードが残りの 3 行になるように、2 行の間のすべてを detailCell.m に移動したいと思います。これを行う最も効果的な方法は何ですか?ありがとうございました。

- (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
4

2 に答える 2

2

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を必要としませんが、私はそれを常にセルに渡す習慣があります:)

于 2015-07-12T16:03:00.363 に答える
0

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;
}
于 2015-07-12T16:01:22.510 に答える