ios - UITableView-Swift : UITableViewCell의 확인 표시

출처 ios uitableview swift ios8 checkmark

Xcode 6.0.1, iOS8 및 Swift를 사용하고 있습니다.
설정-> 일반의 자동 잠금 옵션과 동일한 동작을 UITableView에서 재현해야합니다. Objective-C에서 과거에는

- (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];
}


이것은 훌륭하게 작동합니다!
이제 Swift에서 다음과 같이 썼습니다.

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 시간을 보냈지 만 성공하지 못했습니다
주요 문제는 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
    }
}
관련 기사

ios - Core Data Swift로 TableView 및 행 섹션화

ios - 신속하게 작업 결과에 따라보기 전환

ios - 신속한 iOS에서 텍스트 필드 UI 업데이트

ios - UIView.animateWithDuration 완료

ios - Swift에서 Core Data로부터받은 배열 정렬

ios - UIcollectionview에서 특정 셀에 대한 참조 가져 오기

ios - SWIFT-xcode 6 베타 6.의 KVO. observeValueForKeyPath가 더 이상 호출되지 않습니다.

ios - iOS8에서 VFR-Reader 충돌

ios - iOS 8 확장의 TBXML이 빌드되지 않음

ios - 사진을 찍고 카메라 롤에 저장