2015-06-27 47 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