저는 iOS 개발 초보자입니다. UITableViewCell선택시 확인 표시를 추가하고 싶습니다 . 다른 행을 선택하면 확인 표시를 제거해야합니다. 어떻게해야합니까?
답변
[tableview reloadData];  // 망치를 사용하지 마십시오 .
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   *)indexPath
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}
답변
UITableViewDatasource 메서드에서 :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil )
    {
        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    if ([indexPath compare:self.lastIndexPath] == NSOrderedSame) 
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } 
    else 
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}
// UITableView Delegate Method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.lastIndexPath = indexPath;
    [tableView reloadData];
}
그리고 lastIndexPath는 property(strong)  NSIndexPath* lastIndexPath;
답변
데이터를 다시로드하면 추악한 방식으로 선택 해제 애니메이션이 중단된다는 것을 알았습니다.
이 Swift 구현은 확인 표시를 깔끔하게 추가 / 제거하고 행을 선택 취소합니다.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if self.lastSelection != nil {
        self.myTableView.cellForRowAtIndexPath(self.lastSelection)?.accessoryType = .None
    }
    self.myTableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .Checkmark
    self.lastSelection = indexPath
    self.myTableView.deselectRowAtIndexPath(indexPath, animated: true)
}
어디로 lastSelection선언var lastSelection: NSIndexPath!
추가 활동이 cellForRowAtIndexPath필요하지 않습니다. Obj-C에서 복제하기가 어렵지 않아야합니다.
답변
확인 표시를 설정하려면 :
UITableViewCell *cell = ...;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
셀을 선택 / 선택 취소하려면 :
[cell setSelected:TRUE animated:TRUE]; // select
[cell setSelected:FALSE animated:TRUE]; // deselect
이전 셀을 선택 취소하려면 NSIndexPath * lastSelected ivar를 사용하여 마지막으로 선택한 셀을 추적합니다.
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
   if (self.lastSelected==indexPath) return; // nothing to do
   // deselect old
   UITableViewCell *old = [self.tableView cellForRowAtIndexPath:self.lastSelected];
   old.accessoryType = UITableViewCellAccessoryNone;
   [old setSelected:FALSE animated:TRUE];
   // select new
   UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
   cell.accessoryType = UITableViewCellAccessoryCheckmark;
   [cell setSelected:TRUE animated:TRUE];
   // keep track of the last selected cell
   self.lastSelected = indexPath;
}
답변
Swift 4 업데이트
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
    }
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        tableView.cellForRow(at: indexPath)?.accessoryType = .none
    }
답변
extension ViewController : UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = dataArray[indexPath.row]
        if selectedData.contains(dataArray[indexPath.row]) {
            cell.accessoryType = .checkmark
        }else{
            cell.accessoryType = .none
        }
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if selectedData.contains(dataArray[indexPath.row]) {
            selectedData.removeLast()
            tableView.cellForRow(at: indexPath)?.accessoryType = .none
        }else {
            selectedData.removeAll()
            selectedData.append(dataArray[indexPath.row])
            tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
        }
        print(selectedData)
    }
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        tableView.cellForRow(at: indexPath)?.accessoryType = .none
    }
}
생성 된 dataArray 테이블 뷰를 기반으로 .. 유사하게 빈 배열을 취했고 사용자가 셀을 탭할 때마다 dataArray의 indexValue를 기반으로 해당 객체를 selectedDataArray에 저장했습니다.
질문에 관해서는 … 질문에는 여러 옵션 (답변)이 있지만 결국 하나의 답변 만 결과가됩니다.
마찬가지로, 하나의 셀에만 체크 표시가 나타나야하고 나머지 셀은 선택되지 않아야합니다. 어떤 경우에는 답변을 선택 취소 할 수 있습니다.이 질문에 대한 최선의 답변이 되었기를 바랍니다.
답변
Swift 4.2 및 Swift 5 사용 TableView에서 선택한 행에 대해서만 체크 표시의 작동 코드
   func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    self.tableView.cellForRow(at: indexPath)?.accessoryType = .none
}
   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //print(self.coloursArray[indexPath.row])
     self.tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}


