相关疑难解决方法(0)

如何在Swift中设置UITableViewCellStyleSubtitle和dequeueReusableCell?

我想要一个UITableView使用的带subtitle式单元格dequeueReusableCellWithIdentifier.

我原来的Objective-C代码是:

static NSString*    reuseIdentifier = @"Cell";
    UITableViewCell*    cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
    }
Run Code Online (Sandbox Code Playgroud)

UITableViewSO上搜索了几个问题之后,我想在Swift中写这样的话:

    tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
Run Code Online (Sandbox Code Playgroud)

但这并没有让我说我想要一种subtitle风格.所以我尝试了这个:

var cell :UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
Run Code Online (Sandbox Code Playgroud)

这给了我一个subtitle细胞,但它不让我dequeueReusableCellWithIdentifier.

我已经研究了一些并看了这个视频教程,但他创建了一个单独subclassUITableViewCell,我认为是不必要的,因为我之前在Obj-C中完成了相同的效果.

有任何想法吗?谢谢.

uitableview ios swift

48
推荐指数
6
解决办法
8万
查看次数

标签 统计

ios ×1

swift ×1

uitableview ×1