2016-06-09 67 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

相關問題