[objective-c] UITableView에서 단일 UITableViewCell을 새로 고칠 수 있습니까?

s를 UITableView사용 하는 사용자 정의가 있습니다 UITableViewCell. 각각 UITableViewCell2 개의 버튼이 있습니다. 이 버튼을 클릭하면 UIImageView셀 내의 이미지가 변경됩니다 .

새 이미지를 표시하기 위해 각 셀을 개별적으로 새로 고칠 수 있습니까? 도움을 주시면 감사하겠습니다.



답변

셀의 indexPath가 있으면 다음과 같은 작업을 수행 할 수 있습니다.

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathOfYourCell, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates]; 

Xcode 4.6 이상에서 :

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates]; 

물론 애니메이션 효과로 원하는 것을 설정할 수 있습니다.


답변

방금 전화를 시도했지만 -[UITableView cellForRowAtIndexPath:]작동하지 않았습니다. 그러나 다음은 예를 들어 저에게 효과적입니다. I alloc및 타이트 메모리 관리.releaseNSArray

- (void)reloadRow0Section0 {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    NSArray *indexPaths = [[NSArray alloc] initWithObjects:indexPath, nil];
    [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
    [indexPaths release];
}


답변

빠른:

func updateCell(path:Int){
    let indexPath = NSIndexPath(forRow: path, inSection: 1)

    tableView.beginUpdates()
    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) //try other animations
    tableView.endUpdates()
}


답변

reloadRowsAtIndexPaths:괜찮지 만 여전히 UITableViewDelegate메소드를 강제로 실행 합니다.

내가 상상할 수있는 가장 간단한 방법은 다음과 같습니다.

UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
[self configureCell:cell forIndexPath:indexPath];

그건 중요한 당신의 호출 configureCell:과 같은 주 스레드에서 구현을 비 UI 스레드에서 실 거예요 작업 (와 같은 이야기 reloadData/ reloadRowsAtIndexPaths:). 때로는 다음을 추가하는 것이 도움이 될 수 있습니다.

dispatch_async(dispatch_get_main_queue(), ^
{
    [self configureCell:cell forIndexPath:indexPath];
});

현재 보이는보기 밖에서 수행되는 작업을 피하는 것도 좋습니다.

BOOL cellIsVisible = [[self.tableView indexPathsForVisibleRows] indexOfObject:indexPath] != NSNotFound;
if (cellIsVisible)
{
    ....
}


답변

사용자 정의 TableViewCells를 사용하는 경우 일반

[self.tableView reloadData];    

현재의 견해 를 벗어나서 돌아 오지 않는 한이 질문에 효과적으로 대답하지 못합니다 . 첫 번째 대답도 마찬가지입니다.

를 전환하지 않고 첫 번째 테이블 뷰 셀을 성공적으로 다시로드하려면 다음 코드를 사용하십시오.

//For iOS 5 and later
- (void)reloadTopCell {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    NSArray *indexPaths = [[NSArray alloc] initWithObjects:indexPath, nil];
    [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
}

위의 메소드 를 호출 하는 다음 새로 고침 메소드 를 삽입하여 맨 위 셀 (또는 원하는 경우 전체 테이블보기) 만 사용자 정의 다시로드 할 수 있습니다.

- (void)refresh:(UIRefreshControl *)refreshControl {
    //call to the method which will perform the function
    [self reloadTopCell];

    //finish refreshing 
    [refreshControl endRefreshing];
}

정렬이 완료되면 viewDidLoad다음을 추가하십시오.

//refresh table view
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];

[refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];

[self.tableView addSubview:refreshControl];

이제 상단 셀을 다시로드하는 사용자 정의 새로 고침 테이블 기능이 있습니다. 전체 테이블을 다시로드하려면

[self.tableView reloadData]; 새로운 새로 고침 방법으로

뷰를 전환 할 때마다 데이터를 다시로드하려면 다음 방법을 구현하십시오.

//ensure that it reloads the table view data when switching to this view
- (void) viewWillAppear:(BOOL)animated {
    [self.tableView reloadData];
}


답변

스위프트 3 :

tableView.beginUpdates()
tableView.reloadRows(at: [indexPath], with: .automatic)
tableView.endUpdates()


답변

iOS 6의 새로운 리터럴 구문으로 이러한 답변을 약간 업데이트하려면 단일 객체에 Paths = @ [indexPath]를 사용하거나 여러 객체에 Paths = @ [indexPath1, indexPath2, …]를 사용할 수 있습니다.

개인적으로, 배열과 사전의 문자 구문이 매우 유용하고 시간을 크게 절약한다는 것을 알았습니다. 한 가지만 읽기가 더 쉽습니다. 그리고 그것은 항상 개인적인 bugaboo였던 다중 객체 목록의 끝에 nil이 필요하지 않습니다. 우리 모두 풍차를 기울여야합니다 😉

내가 이것을 믹스에 넣을 것이라고 생각했습니다. 도움이 되길 바랍니다.