ios - 多个 TableView 行选择 - Swift

标签 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)

    }

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

最佳答案

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

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

} else {
    [cell setAccessoryType:UITableViewCellAccessoryNone];
}

你可以快速完成,逻辑是一样的

关于ios - 多个 TableView 行选择 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27397387/

相关文章:

ios - 如何在 iOS 应用程序中创建一个包含 3 列的表格?( Objective-C )

ios - 了解 iOS 中的 View Controller 嵌套

ios facebook sdk 4.0 登录错误代码 304

ios - 游戏中心不保存分数

ios - 从 UITableView 删除一行时 Swift 应用程序崩溃

iphone - 从 parse.com 检索图像

ios - 在 iPhone X 模拟器的顶部和底部看到黑条

ios - 移除自定义导航条背面图像阴影

ios - Swift UIImage 转换为 PDF

ios - 在 Swift 的导航栏中动画更改图像