注册cell可重用单元格注意事项

走远了吗. 提交于 2020-03-02 18:21:05

在storyboard中已经注册了cell的可重用标识符 点击cell时 push到下一个视图控制器后  

如果 再用代码 注册一边cell的可重用单元格  会覆盖之前

// 视图被加载完成之后被调用,stroyboary中的原型cell,已经存在
- (
void)viewDidLoad
{
    [
super viewDidLoad];
   
   
// tableView注册可重用单元格,使用Cell作为可重用标识符,会覆盖之前在storyboard中注册的原型cell
   
// 并没有segue连线,所以再点击tableViewCell,就不会跳转了
    [
self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}


解决方法可以在UITableView的代理方法选中某行时  

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;{


   
CZWebViewController* vc = [[CZWebViewController alloc] init];
   
    [
self.navigationController pushViewController:vc animated:YES];

    // 拼接文件全路径 传给目标控制器
    vc.
fullPath = [self.fileDir stringByAppendingPathComponent:self.files[indexPath.row]];
}

如果用stroyboard注册cell时 用下面的方法给目标控制器传值

// 用来给目标视图控制器赋值的,用storyborad开发
- (
void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   
CZWebViewController *vc = segue.destinationViewController;
   
   
// 1. 知道用户当前选中的行
   
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
   
NSLog(@"%@", self.files[indexPath.row]);

    vc.
fullPath = [self.fileDir stringByAppendingPathComponent:self.files[indexPath.row]];

}


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!