UITableView 中数组内部的数组?

Array inside of array in a UITableView?

假设我有 3 个数组,我想在 UITableView 中显示它们。该数组由 NSString 组成。但是数组的长度并不完全相同。将它们放入 UITableView 的最佳方法是什么,每个数组都有部分?我认为您可以在一个数组中放置多个数组来帮助解决这个问题?请为菜鸟提供详细的答案。 :)

let name = ["Name", "Address","Phone", "DOB"]
let work = ["Name", "Address","Main Phone", "Manager Name", "Manager Phone"]
let property = ["Address","State", "Zip"]

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 3

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        if let indexPath = self.tableView.indexPathForSelectedRow() {
            let object = name[indexPath.row] as String
            let controller = (segue.destinationViewController as UINavigationController).topViewController as DetailViewController
            controller.detailItem = object
            controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
            controller.navigationItem.leftItemsSupplementBackButton = true
        }
    }
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if (section == 0) {
        return name.count
    }else if (section == 1) {
        return work.count
    }else if (section == 2) {
        return property.count;
    }else{
        return 1
    }
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {


    if(section == 0){
        title = "Personal Info"
    }else if (section == 1){
        title = "Work Info"
    }else if (section == 2){
        title = "Property"
    }

    return title
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

    let NameInfo = name[indexPath.row]
    let WorkInfo = work[indexPath.row]
    let rentalInfo = property[indexPath.row]


    if (indexPath.section == 0){
        cell.textLabel!.text = NameInfo
    }else if (indexPath.section == 1){
        cell.textLabel!.text = WorkInfo
    }else if (indexPath.section == 2){
        cell.textLabel!.text = rentalInfo
    }

    return cell
}

如果每个数组的长度不同,我会收到此错误:致命 error:Array 索引超出范围。

去掉中间变量(NameInfo、WorkInfo、rentalInfo)。由于无论当前请求是针对哪个部分,您都将分配给那些部分,因此对于长度不均匀的数组,一个或多个将是一个不好的参考。

根据部分 (cell.textLabel!.text = name[indexPath.row]) 选择数组,您将不会在 array/section 不匹配的情况下提取数据。

或者,如果您要重用逻辑,您可以创建一个函数来执行此操作。

控制器内部的实例变量class:

var myArrays: [[String]] = []

内部初始化awakeFromNib:

    myArrays.append(name)
    myArrays.append(work)
    myArrays.append(property)

函数:

func textForIndexPath(path:NSIndexPath) -> String {
    var result = "Bad path"

    if path.section >= 0 && path.section < myArrays.count {
        let theArray = myArrays[path.section]
        if path.row >= 0 && path.row < theArray.count {
            result = theArray[path.row]
        }
    }
    return result
}