【问题标题】:How to make a tableView with different detail view for each cell如何为每个单元格制作具有不同详细视图的 tableView
【发布时间】:2014-12-12 13:00:10
【问题描述】:

我写的是:

    func initializeMyDictionary() {
            self.example = [MyDictionary(name: "Some Name", text: "some text", link: "linkToSomeDocument"),
MyDictionary(name: "Second Name", text: "some text", link: "secondLink")]
        }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let identifier: String = "tableCell"

        var cell: TableCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? TableCell

        if cell == nil {

            cell = TableCell(style: UITableViewCellStyle.Value1, reuseIdentifier: identifier)

        }

        cell!.nameLabel!.text = example[indexPath.row].name

        return cell!

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return example.count
    }

然后将它链接到 Main.storyboard 上的 DetailViewController(我创建的)。每个 myDictionary 字符串的 DetailView 都显示在我的 tableView 中。一切正常。

但我想从 MyDictionary(链接)中显示一些行的第二个值。 1 行显示 MyDictionary 中的“名称”和“文本”,2 行显示“链接”中的文档或 URL。这是不可能的还是如何做这样的事情?

我很抱歉我的无知和糟糕的英语。

【问题讨论】:

  • 尝试使用UITableViewCellStyle.Subtitle而不是UITableViewCellStyle.Value1并通过cell!.textLabel!.text = example[indexPath.row].name访问名称标签和cell!.detailTextLabel!.text = example[indexPath.row].link这样的字幕标签希望这是你想要的
  • @anneblue 感谢您的回答,但我想从单元格中打开不同的 DetailView,以便在此单元格中使用不同的内容。例如:#1、#2、#3、#4 和 #6 单元格打开了我创建的 DetailViewController(这对我来说没有任何问题);单元格#5 我想打开绑定到它的 RTF 文档。但我的问题如下:每个单元格都打开 DetailViewController,但我无法将其中一个单元格设为单独的。

标签: ios uitableview swift tableview tablecell


【解决方案1】:

两种方法:

1.

删除从原型单元到视图控制器的故事板segue,只需实现UITableViewDelegate 方法-(void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath 方法并将另一个视图控制器推到那里,例如:

- (void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath 
{
    // the identifier of the viewController in storyboard
    NSString* identifier = nil;
    // section 0
    switch (indexPath.section) {
        case 0: 
            // rows in section 0
            switch (indexPath.row) { 
                // row 0 in section 0
                case 0: identifier = @"IdentifierOfVCForFirstCellInFirstSection"; break;
                case 1: identifier = @"IdentifierOfVCForSecondCellInFirstSection"; break;
                default: break;
            } 
            break;
        default: break;
    }
    // 
    if (identifier != nil) {
        // perform Segue programmatically 
        [self.navigationController pushViewController: [[self storyboard] instantiateViewControllerWithIdentifier: identifier] animated: YES];
    }
    else {
        NSAssert(NO, @"No storyboard identifier implemented for row %@ in section %@", @(indexPath.row), @(indexPath.section));
    }
}

或 2。

在 IB 中将另一个原型单元格添加(拖放)到您的 tableView 中。在属性检查器中给它一个不同的重用标识符。 然后将它与您想要的不同视图控制器连接(右键单击并在'Triggered Segues' 下将selection 拖到您想要的视图控制器。

然后,在cellForRowAtIndexPath 方法中,根据您的 indexPath 决定您想要出列哪种UITableViewCell

对于第二个解决方案,我建议对原型单元格使用自定义 UITableView 子类/Xib。

【讨论】:

  • 我是 Swift 和 Objective-C 的新手,对我来说太难了。在了解如何执行此操作后,我将尝试您的第二种解决方案。或者您可以自己编辑 cellForRowAtIndexPath 吗?或者更多解释一下。很抱歉,谢谢你的帮助。
  • 如果只使用一个,为什么还要初始化两个 VC?
  • @ZaneHelton 你是完全正确的,相应地改变了答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多