如何从swift的tableview的每个部分中select一行?

我想从同一个表视图的不同部分中select一行。 我得到许多行正在select的输出,但我只想从每个部分只有一个选定的行。

这是我的阵容:

var filterFeedUnderAll = ["Complex","NotComplex","Easy"] var filterFeedUnderAllStocks = ["AllStocks","Portfolio","Watchlist","Sector","Ticker"] var filterFeedUnderByDate = ["ByDate","ByComments","ByLikes"] 

我用过的方法:

 func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 3 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { var count:Int? if section == 0 { count = filterFeedUnderAll.count } else if section == 1 { count = filterFeedUnderAllStocks.count } else if section == 2 { count = filterFeedUnderByDate.count } return count! } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = self.m_HomeFeedFilterBaseTableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! HomeFeedFIlterBaseTableViewCell switch (indexPath.section) { case 0: cell.m_TableItemsLabel.text = filterFeedUnderAll[indexPath.row] case 1: cell.m_TableItemsLabel.text = self.filterFeedUnderAllStocks[indexPath.row] case 2: cell.m_TableItemsLabel.text = filterFeedUnderByDate[indexPath.row] default: cell.m_TableItemsLabel.text = "Other" } return cell } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let cell = m_HomeFeedFilterBaseTableView.cellForRowAtIndexPath(indexPath) as! HomeFeedFIlterBaseTableViewCell for selectedIndexPath: NSIndexPath in tableView.indexPathsForSelectedRows! { if selectedIndexPath.section == indexPath.section { cell.m_TableItemsLabel.textColor = selectedTextColor tableView.deselectRowAtIndexPath(indexPath, animated: true) } } } 

我想从每个部分select一行。 帮助我完成这个任务。

首先你需要在你的tableView启用多个select,然后这是我用来做这个的代码,请注意,我使用了一个名为selectedRows格式为[String:NSIndexPath]Dictionary ,我在这里存储一个indexPath。在addSelectedCellWithSection

 import UIKit class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate { @IBOutlet weak var tableView: UITableView! var filterFeedUnderAll = ["Complex","NotComplex","Easy"] var filterFeedUnderAllStocks = ["AllStocks","Portfolio","Watchlist","Sector","Ticker"] var filterFeedUnderByDate = ["ByDate","ByComments","ByLikes"] var selectedRows = [String:NSIndexPath]() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 3 } func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 50; } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { var count:Int? if section == 0 { count = filterFeedUnderAll.count } else if section == 1 { count = filterFeedUnderAllStocks.count } else if section == 2 { count = filterFeedUnderByDate.count } return count! } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = self.tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! testCell switch (indexPath.section) { case 0: cell.lblName.text = filterFeedUnderAll[indexPath.row] case 1: cell.lblName.text = self.filterFeedUnderAllStocks[indexPath.row] case 2: cell.lblName.text = filterFeedUnderByDate[indexPath.row] default: cell.lblName.text = "Other" } return cell } func addSelectedCellWithSection(indexPath:NSIndexPath) ->NSIndexPath? { let existingIndexPath = selectedRows["\(indexPath.section)"]; if (existingIndexPath == nil) { selectedRows["\(indexPath.section)"]=indexPath; }else { selectedRows["\(indexPath.section)"]=indexPath; return existingIndexPath } return nil; } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! testCell let previusSelectedCellIndexPath = self.addSelectedCellWithSection(indexPath); if(previusSelectedCellIndexPath != nil) { let previusSelectedCell = self.tableView.cellForRowAtIndexPath(previusSelectedCellIndexPath!) as! testCell previusSelectedCell.lblName.textColor = UIColor.blackColor(); cell.lblName.textColor = UIColor.redColor() tableView.deselectRowAtIndexPath(previusSelectedCellIndexPath!, animated: true); } else { cell.lblName.textColor = UIColor.redColor(); } } } 

希望这可以帮助你,对我来说是完美的