2014-10-07 3 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; 
    } 
} 

을 그리고이 작동하지 않습니다. 체크 표시는 때로 만 나타나며 스크롤 할 때 사라집니다. 나는 왜 성공하지만 실패했는지 알아 내기 위해 2 시간을 보냈다.

+5

그리고 lastSelectedIndexPath 선언 할 예정이다 : 여기

몇 가지 정리입니까? – Josh

답변

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가 0이 될 수 있기 때문입니다. – Giorgio

+0

그래, 그걸 처리해야하는'lastSelectedIndexPath? .row == row'에'?'가 있습니다 : – fluidsonic

+0

두 코드 스 니펫은 동일합니까? 귀하의 코드 조각을 사용하려고했지만 결과는 같습니다. – Giorgio