繁体   English   中英

对许多UITableView使用自定义UITableViewCell

[英]Use a custom UITableViewCell for many UITableView

我通过将标签和imageView从我的原型单元拖放到我称为QuizInfoCell的自定义单元中,为UITableView创建了一个自定义UITableViewCell

这对于我在其中创建了原型单元的特定TableView很好用,但是我无法让QuizInfoCell在任何其他TableView. 我该如何实现?

这是我在创建原型单元的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;
}

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

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

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

通过拖放标签和imageView,以与以前相同的方式重新创建自定义UITableViewCell

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

在此处输入图片说明

然后,您可以使用代码加载新的Custom 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;
}

暂无
暂无

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

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