IT

UITableView에서 단일 UITableViewCell을 새로 고칠 수 있습니까?

lottoking 2020. 5. 21. 08:25
반응형

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

Now that you have that sorted, inside of your viewDidLoad add the following:

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

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

[self.tableView addSubview:refreshControl];

You now have a custom refresh table feature that will reload the top cell. To reload the entire table, add the

[self.tableView reloadData]; to your new refresh method.

If you wish to reload the data every time you switch views, implement the method:

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

Swift 3 :

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

Just to update these answers slightly with the new literal syntax in iOS 6--you can use Paths = @[indexPath] for a single object, or Paths = @[indexPath1, indexPath2,...] for multiple objects.

Personally, I've found the literal syntax for arrays and dictionaries to be immensely useful and big time savers. It's just easier to read, for one thing. And it removes the need for a nil at the end of any multi-object list, which has always been a personal bugaboo. We all have our windmills to tilt with, yes? ;-)

Just thought I'd throw this into the mix. Hope it helps.


I need the upgrade cell but I want close the keyboard. If I use

let indexPath = NSIndexPath(forRow: path, inSection: 1)
tableView.beginUpdates()
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) //try other animations
tableView.endUpdates()

the keyboard disappear


Here is a UITableView extension with Swift 5:

import UIKit

extension UITableView
{    
    func updateRow(row: Int, section: Int = 0)
    {
        let indexPath = IndexPath(row: row, section: section)

        self.beginUpdates()
        self.reloadRows(at: [indexPath as IndexPath], with: UITableView.RowAnimation.automatic)
        self.endUpdates()
    }

}

Call with

self.tableView.updateRow(row: 1)

참고URL : https://stackoverflow.com/questions/4448321/is-it-possible-to-refresh-a-single-uitableviewcell-in-a-uitableview

반응형