2015-06-27 49 views
0

我覺得這是一個愚蠢的問題,但我不能讓它工作。我希望有一個標題的單元匹配某個字符串。如果這個單元格匹配那個字符串,我希望單元格被突出顯示。但是,當我運行我的應用程序時,單元格被選中(像普通表格視圖中突出顯示的灰色)一秒鐘,然後消失。這是我的嘗試。設置UITableViewCell在代碼中選擇

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
var cell = tableView.dequeueReusableCellWithIdentifier("RouteCell") as! UITableViewCell 

var singleStop = self.stops[indexPath.row] 

if(singleStop.stop_name == routeLabelName) // string comparison 
{ 
    cell.highlighted = true 
    cell.selected = true 

} 

cell.textLabel!.text = singleStop.stop_name 

cell.imageView!.image = UIImage(named: "ArrowPathMiddle.png") 

return cell 
} 
+0

試試我的片段,我已經通過的Xcode 6.3.2 +迅速檢查了它。它適用於我 – Doro

回答

1

對你的看法設置multipleselection didload

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.tableView.allowsMultipleSelection = true; 
} 

下一個重寫代碼爲

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
var cell = tableView.dequeueReusableCellWithIdentifier("RouteCell") as! UITableViewCell 

var singleStop = self.stops[indexPath.row] 


if (singleStop.stop_name == routeLabelName) 
    { 
     self.tableView .selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None) 
    } 
    else 
    { 
     self.tableView .deselectRowAtIndexPath(indexPath, animated: false) 
    } 

cell.textLabel!.text = singleStop.stop_name 

cell.imageView!.image = UIImage(named: "ArrowPathMiddle.png") 

return cell 
} 
+0

謝謝你親切先生! – applejuiceteaching

+0

歡迎您:) – Doro