2016-06-09 80 views
1

我想从同一个表视图的不同部分中选择一行。我得到许多行正在选择的输出,但我只需要每个部分中只有一个选定的行。如何从swift的tableview的每个部分中选择一行?

这里是我的数组:

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) 
     } 
    } 

} 

我要选择从每节一行。帮助我完成这项任务。

+1

如果我理解正确:你想从你的tableview实现多重选择,对不对? –

+0

对于每个部分,您只需要选择一行。我对吗? –

回答

3

首先你需要启用多重选择在您的tableView,然后这是我以前做的是我用格式[String:NSIndexPath]一个Dictionary名为selectedRows,我的部分存储一个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(); 
     } 

    } 

} 

希望这可以帮助您,为我的作品完美

+0

谢谢你梅利安......现在正在工作。 – GRDY

相关问题