2014-10-07 57 views
3

我正在使用Xcode 6.0.1,iOS8和Swift。 我必須重現我的UITableView相同的行爲自動鎖選項在設置 - >一般。在Objective-C中,我寫過UITableView-Swift:UITableViewCell中的選中標記

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CheckMarkCellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CheckMarkCellIdentifier] autorelease]; 
    } 
    cell.textLabel.text = ... 
    cell.detailTextLabel.text = ...; 
    cell.accessoryType = [indexPath isEqual: self.lastSelectedIndexPath] ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; 
    return cell; 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    int newRow = indexPath.row; 
    int oldRow = self.lastSelectedIndexPath.row; 
    if (newRow != oldRow) { 
     UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; 
     newCell.accessoryType = UITableViewCellAccessoryCheckmark; 
     UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastSelectedIndexPath]; 
     oldCell.accessoryType = UITableViewCellAccessoryNone; 
     self.lastSelectedIndexPath = indexPath; 
    } 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

這很好用! 現在,在斯威夫特,我寫道:

override func viewDidLoad() { 
    super.viewDidLoad() 
    ... 
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "categoryCell") 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("categoryCell", forIndexPath: indexPath) as UITableViewCell 
    cell.textLabel?.text = categories[indexPath.row] 
    let row = indexPath.row; 
    if let lastIndexPath = self.lastSelectedIndexPath { 
     cell.accessoryType = (lastIndexPath.row == row) ? UITableViewCellAccessoryType.Checkmark : UITableViewCellAccessoryType.None; 
    } 
    return cell 
} 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    self.tableView.deselectRowAtIndexPath(indexPath, animated: true) 

    let newRow = indexPath.row; 
    var oldRow: Int? 
    if let lastIndexPath = self.lastSelectedIndexPath { 
     oldRow = lastIndexPath.row; 
     let oldCell = self.tableView(tableView, cellForRowAtIndexPath: lastIndexPath) 
     oldCell.accessoryType = UITableViewCellAccessoryType.None; 
    } 
    if (newRow != oldRow) { 
     let newCell = self.tableView(tableView, cellForRowAtIndexPath: indexPath) 
     newCell.accessoryType = UITableViewCellAccessoryType.Checkmark; 
     self.lastSelectedIndexPath = indexPath; 
    } 
} 

而這是行不通的。勾選標記有時只會出現,並且在滾動時會消失。我花了兩個小時找出原因,但沒有成功。

+5

在哪裏,lastSelectedIndexPath應該被宣佈? – Josh 2015-06-16 14:43:20

回答

11

主要問題是您撥打tableView(_:cellForRowAtIndexPath:)而不是tableView.cellForRowAtIndexPath(_:),因此創建一個新的單元而不是獲取當前顯示的單元。

下面是一些清理:

override func viewDidLoad() { 
    super.viewDidLoad() 

    // … 

    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "categoryCell") 
} 


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("categoryCell", forIndexPath: indexPath) as UITableViewCell 
    cell.accessoryType = (lastSelectedIndexPath?.row == indexPath.row) ? .Checkmark : .None 
    cell.textLabel?.text = categories[indexPath.row] 

    return cell 
} 


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    tableView.deselectRowAtIndexPath(indexPath, animated: true) 

    if indexPath.row != lastSelectedIndexPath?.row { 
     if let lastSelectedIndexPath = lastSelectedIndexPath { 
      let oldCell = tableView.cellForRowAtIndexPath(lastSelectedIndexPath) 
      oldCell?.accessoryType = .None 
     } 

     let newCell = tableView.cellForRowAtIndexPath(indexPath) 
     newCell?.accessoryType = .Checkmark 

     lastSelectedIndexPath = indexPath 
    } 
} 
+0

我用可選的綁定'因爲self.lastSelectedIndexPath可以爲零 – Giorgio 2014-10-07 14:50:43

+0

是的,這就是爲什麼有'?'在'lastSelectedIndexPath?.row ==行'應該照顧的:) :) – fluidsonic 2014-10-07 14:52:43

+0

所以這兩個代碼片段是等價的?我試圖使用你的代碼片段,但結果是一樣的。 – Giorgio 2014-10-07 14:57:18