多个TableView行选择 - Swift

时间:2014-12-10 09:27:23

标签: ios swift

我正在使用Swift。我有tableView,我希望用户选择多个选择。当用户点击一行时,我希望它突出显示,然后允许他们选择更多行。

我的代码如下:

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        //        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
        var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell

        var allCreditCard : AllCreditCard

        allCreditCard = self.creditCard[indexPath.row]

        cell.textLabel?.text = allCreditCard.bankName


//        if(cell.selected){
//            println("cell selected at \(indexPath.row)");
//            let imageName = "tick.png";
//            let image: UIImageView = UIImageView(image: UIImage(named: imageName));
//            cell.accessoryView = image;
//        }else{
//            println("cell was NOT selected. Index path----- \(indexPath.row)");
//            let image: UIImageView = UIImageView();
//            cell.accessoryView = image;
//        }


        return cell

    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


        tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)

    }

我该如何解决这个问题。我不确定如何添加rowIndex es,然后使用更新的行填充表。有人可以帮助我。

1 个答案:

答案 0 :(得分:3)

获取一个数组并在indexPath.row上添加所有选定的didSelectRowAtIndexPath 并在cellForRowAtIndexPath中检查该数组中的所有对象,并将单元格的AccessoryType设置为UITableViewCellAccessoryCheckmark

if (selectedIndex==indexPath.row) { 
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];

} else {
    [cell setAccessoryType:UITableViewCellAccessoryNone];
}

你可以在swift中做到,逻辑是相同的

相关问题