데이터 소스에서 셀을 가져 오지 못했습니다.
아래 코드의 문제점
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = @"hello";
return cell;
}
과
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 2;
}
하지만 데이터 소스에서 셀을 가져 오지 못했습니다.
전체 예외는
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (<UITableView: 0x7ffe0b092800; frame = (0 97; 414 590); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7ffe0acf3b10>; layer = <CALayer: 0x7ffe0aceb6e0>; contentOffset: {0, 0}; contentSize: {414, 88}>) failed to obtain a cell from its dataSource (<AllContactsViewController: 0x7ffe0ace60f0>)'
테이블보기에 대리자 및 데이터 원본을 설정했습니다.
귀하의 오류는 어떤 이유로 인해 cellForRowAtIndexPath
반환 된다는 것을 암시 nil
하며 재사용 가능한 셀을 대기열에서 빼지 못했기 때문이라고 생각합니다. 이를 확인하려면 현재 대기열에서 빼기 호출 후에 중단 점을 cell
설정하십시오 nil
. 이 (가)로 설정되어 있을 것으로 예상합니다 .
프로토 타입 셀을 만든 최신 Xcode 템플릿을 사용하는 경우 대신 다음을 사용해야합니다.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
Xcode 템플릿을 사용하지 않는 경우 어쨌든 해당 코드 줄을 사용한 다음 다음과 같이 고유 한 재사용 식별자를 등록합니다.
[self.tableView registerClass:[UITableViewCell self] forCellReuseIdentifier:@"Cell"];
문제를 해결해야하는 모든 것이 잘됩니다. 나는 Swift 사용자를 위해 이것을 더 자세히 썼습니다.
Swift 3를 사용하여 UITableViewDataSource 및 UITableViewDelegate 프로토콜을 ViewController 선언에 추가하는 것을 잊었을 때이 오류가 발생했습니다.
클래스 DataViewController : UIViewController, UITableViewDataSource , UITableViewDelegate
그것들을 추가하면 문제가 해결되었습니다.
스위프트 3.0
UITableViewDelegate 및 UITableViewDataSource 를 추가하기 만하면됩니다.
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
아래 이미지는 참고 용입니다.
셀에 식별자를 제공해야합니다. "셀"은 식별자 필드에있는 셀의 속성 검사기에서와 같이 셀의 식별자를 제공합니다. 귀하의 오류를 재현했으며 귀하의 셀에 식별자를 제공하지 않았기 때문입니다.
나는 같은 문제가 있었고 UITableViewDataSource 및 UITableViewDelegate 프로토콜을 구현하는 것을 잊었 기 때문입니다.
상속 된 클래스 뒤에 클래스 선언에 추가하십시오.
xcode는 내 코드에서 UITableViewDataSource 및 UITableViewDelegate 메서드를 사용했기 때문에 이에 대해 경고해야합니다.
제 경우에는 UITableview에 대해 UITableViewDataSource, UITableViewDelegate를 선언하는 것을 잊었습니다.
제 경우에는 동적 프로토 타입에서 정적 셀로 변경하는 것을 잊었습니다. 인터페이스 빌더에서이 부분을 확인하십시오.
또한 아래 코드를 시도하십시오
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
// Reuse and create cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Update cell data contents
cell.textLabel.text = @"Your text here";
cell.detailTextLabel.text=@"Your detailed text label";
return cell;
}
추가 된 UITableViewDataSource가 추가되었는지 확인하십시오. 이걸 놓쳐서 앱이 다운되었습니다.
UITableViewDelegate 및 UITableViewDataSource 프로토콜을 구현하는 것을 잊지 않았는지 확인하십시오.
class ExampleViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
Careful when you are migrating
In case anyone is migrating their code from older swift versions to newer ones, this may occur if you unintentionally don't change UITableViewDataSource
functions' syntax to the newer swift syntax.
Unfortunately the compiler won't complain about it.
I faced this when I was migrating my code from swift 2 to swift 4. I didn't change cellForRow DataSource method syntax from:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
to its swift 4 version:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
and I got the error:
"Failed to obtain a cell from its DataSource"
Probably you are not using the same Identifier you have given in the main story board for prototype cell. Try to telly the name for Upper case lower case as well to make sure you are using correct identifier.
Good Luck:)
If you are not loading Nib then you have to first register Nib in viewDidLoad.
[self.tableView registerNib:[UINib nibWithNibName:@"RecruitmentDetailViewC" bundle:nil] forCellReuseIdentifier:@"RecruitmentDetailViewC"];
When using tableViewCell apart from the UITableView you have to register the NIB yourself. When the NIB is not registered the "Failed to obtain a cell from its DataSource" error may occur. From the error description I always find it hard to figure out, what is wrong.
This error can easily be solved by registering:
[self registerNib:[customerButtonTableViewCell class]
forCellReuseIdentifier:customerButtonCellIdentifier forTableView:tableView];
After having done the steps suggested in the other posts, and checking if my cell was nil, it my problem was not resolved and my cell was not showing as nil when I checked it. However, adding this handy code snippet in there did resolve the issue, so it must have been nil at some point where I wasn't catching it.
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ClassNameOfMyCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
You must override 'cellForRowAt' method. Take care if you didn't do it
참고 URL : https://stackoverflow.com/questions/34250595/failed-to-obtain-a-cell-from-its-datasource
'Nice programing' 카테고리의 다른 글
바운스 각도를 계산하는 방법? (0) | 2020.11.25 |
---|---|
Rails 3 요일 찾기 (0) | 2020.11.25 |
PHP를 사용하여 분을 시간과 분으로 변환 (0) | 2020.11.25 |
xcrun 개발자 경로 변경 (0) | 2020.11.25 |
gem을 사용하여 Ubuntu에 Rails를 설치하는 방법 (0) | 2020.11.25 |