![](/img/trans.png)
[英]How to use a custom UITableViewCell with xib without a UITableView
[英]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文件。
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.