对许多UITableView使用自定义UITableViewCell

时间:2013-10-29 11:59:10

标签: ios objective-c uitableview

我已经为UITableViewCell创建了一个自定义UITableView,方法是将标签和imageView从我的原型单元格拖放到我称之为QuizInfoCell的自定义单元格。

对于我在其中创建原型单元格的特定TableView工作正常,但我不能让QuizInfoCell为其他任何TableView.工作我怎样才能实现?< / p>

这是我在创建Prototype Cell的TableView中使用的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QuizInfoCell *cell = nil;       
    cell = [tableView dequeueReusableCellWithIdentifier:@"QuizInfoCell" forIndexPath:indexPath];

    if (cell == nil){
        cell = [[QuizInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"QuizInfoCell"];
    }

    cell.name.text = [title objectAtIndex:indexPath.row];
    cell.author.text = [author objectAtIndex:indexPath.row];
    cell.time.text = [[ValueHandler alloc] getDateString:[timeStamp objectAtIndex:indexPath.row]];
    cell.plays.text = [@"Plays: " stringByAppendingString:[NSString stringWithFormat:@"%@", [plays objectAtIndex:indexPath.row ]]];
    cell.ratingUps.text = [NSString stringWithFormat:@"%@", [ratingUps objectAtIndex:indexPath.row]];
    cell.ratingDowns.text = [NSString stringWithFormat:@"%@", [ratingDowns objectAtIndex:indexPath.row]];
    cell.qid.text = [NSString stringWithFormat:@"%@", [qid objectAtIndex:indexPath.row]];


    return cell;
}

1 个答案:

答案 0 :(得分:0)

我建议您将QuizInfoCell创建为新的XIB文件。

  • 按CMD + N
  • 选择User Interface子菜单。
  • 创建一个新的View并将其命名为QuizInfoCell

在新的XIB中删除现有的View元素,然后拖放新的Table View Cell

通过拖放标签和imageView

,以与之前相同的方式重新创建自定义UITableViewCell

最后给新的Cell一个名为QuizInfoCell的标识符,如下所示:

enter image description here

然后,您可以使用代码加载新的自定义UITableViewCell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QuizInfoCell *cell = nil;       
    cell = [tableView dequeueReusableCellWithIdentifier:@"QuizInfoCell" forIndexPath:indexPath];

    if (cell == nil){
        NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"QuizInfoCell" owner:self options:nil];
        cell = [nibContents objectAtIndex:0];
    }

    cell.name.text = [title objectAtIndex:indexPath.row];
    cell.author.text = [author objectAtIndex:indexPath.row];
    cell.time.text = [[ValueHandler alloc] getDateString:[timeStamp objectAtIndex:indexPath.row]];
    cell.plays.text = [@"Plays: " stringByAppendingString:[NSString stringWithFormat:@"%@", [plays objectAtIndex:indexPath.row ]]];
    cell.ratingUps.text = [NSString stringWithFormat:@"%@", [ratingUps objectAtIndex:indexPath.row]];
    cell.ratingDowns.text = [NSString stringWithFormat:@"%@", [ratingDowns objectAtIndex:indexPath.row]];
    cell.qid.text = [NSString stringWithFormat:@"%@", [qid objectAtIndex:indexPath.row]];


    return cell;
}