2017-10-06 86 views

回答

0

你可以使用這樣的:

var lastSelectedIndexPath: IndexPath? 

func viewDidLoad() { 
    Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(timerFunc), userInfo: nil, repeats: true) 
} 
@objc func timerFunc() { 
    if lastSelectedIndexPath != nil { 
     tableView(self.table, didUnhighlightRowAt: lastSelectedIndexPath!) 
    } 
    tableView(self.table, didHighlightRowAt: IndexPath(row: YOUR_ROW, section: YOUR_SECTION)) 
} 

func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) { 
    let cell = tableView.cellForRow(at: indexPath) 
    cell?.backgroundColor = UIColor.red 
    lastSelectedIndexPath = indexPath 
} 

func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) { 
    let cell = tableView.cellForRow(at: indexPath) 
    cell?.backgroundColor = UIColor.clear 
} 
0

我回答這個問題是基於我對這個問題的理解。假設在一段時間後突出顯示tableview單元格。

[NSTimer scheduledTimerWithTimeInterval:2.0 
    target:self 
    selector:@selector(targetMethod:) 
    userInfo:nil 
    repeats:YES]; 


- (void)targetMethod { 
NSIndexPath *defaultIndexPath = [NSIndexPath indexPathForRow:Any_Row_Number inSection:Any_Section_Number]; 
[self tableView:[self tableView] didSelectRowAtIndexPath:defaultIndexPath]; 
}