【问题标题】:How can I choose multiple rows and send those options to a second UITableView through a prepareForSegue function?如何选择多行并通过 prepareForSegue 函数将这些选项发送到第二个 UITableView?
【发布时间】:2015-12-19 15:57:38
【问题描述】:

我在一个包含 5 行(“a”到“e”)的 UITableView 中工作,当我单击其中一行时,它会在下一个 UITableView 中显示我选择作为标题的字母(带有prepareForSegue 函数)。实际上,直到这一点它完美地工作..但是当我在第一个 UITableView 中单击多行后尝试在第二个 tableView 中添加更多“标题”时,它只会显示最后一个选项(例如,如果我单击首先是“a”,然后是“d”,然后是“c”……第二个 UITableView 只显示行“c”作为标题,而不是其他 2 行)。

如何选择多行并通过 prepareForSegue 函数将这些选项发送到第二个 UITableView?

第一个视图的代码行如下:

let array = ["a", "b", "c", "d", "e"]

@IBOutlet var initialTableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "segueA" {
        if let destination = segue.destinationViewController as? Content {
            if let selectedRow = initialTableView.indexPathForSelectedRow?.row {
                destination.title = array[selectedRow]
            }
        }

    }
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cellA", forIndexPath: indexPath) as UITableViewCell
    cell.textLabel!.text = array[indexPath.row]
    return cell
}

第二个视图包含以下几行:

var contentArray:[String] = []

@IBOutlet var contentTableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    contentArray.append(title!)
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return contentArray.count
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 2
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellContent = tableView.dequeueReusableCellWithIdentifier("cellContent", forIndexPath: indexPath) as UITableViewCell
    cellContent.textLabel!.text = "Second test"
    return cellContent
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return contentArray[section]
}

谢谢!

【问题讨论】:

    标签: ios swift uitableview segue


    【解决方案1】:

    乔。

    为了解决您的问题。您应该通过以下方式启用多项选择:

    tableView.allowsMultipleSelection = YES;

    你应该记住你在第一个 viewContoller 中选择的项目。为方便起见,请尝试使用:

    var indexPathsForSelectedRows: [NSIndexPath]? { get }
    

    然后通过 segue 将此信息发送给另一个 viewController

    例子:

    let array = ["a", "b", "c", "d", "e"]
    
    @IBOutlet var initialTableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initialTableView.allowsMultipleSelection = YES
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "segueA" {
            if let destination = segue.destinationViewController as? Content {
                if let selectedRows = initialTableView.indexPathsForSelectedRows {
                    //hear you have option to send items that are selected to another controller or send only indexPath.
                }
            }
    
        }
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.array.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cellA", forIndexPath: indexPath) as UITableViewCell
        cell.textLabel!.text = array[indexPath.row]
        return cell
    }
    

    【讨论】:

    • 感谢您的回答。所以,当我写if let selectedRows = initialTableView.indexPathsForSelectedRows { destination.title = array[selectedRows] } 时……现在给我一个错误:“不能用[NSIndexPath] 类型的索引来下标[String] 类型的值”
    【解决方案2】:

    您必须自己将选定的行存储在列表或数组中

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //check if self._selectedIndices (your array) contains indexPath.row
        //if yes then remove it from array
        cell.accessoryType = UITableViewCellAccessoryType.None // remove check mark if you want to use it
        //if no then add it to array
        cell.accessoryType = UITableViewCellAccessoryType.Checkmark // add check mark if you want to use it
    }
    

    然后在你的 prepareForSegue

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // use self._selectedIndices to get the selected row indices
        // self._dataSource[index] for the actual data
    }
    

    【讨论】:

      【解决方案3】:

      只需在 tableview 上启用多项选择,并使用 indexPathsForSelectedRows 变体来获取完整列表。然后,您可以从数据源 + 索引传递整个列表,或者将索引映射到实际值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多