2014-01-29 2 views
9

인 경우 간단한 문제가 있지만 이해가되지 않습니다. tableView을 포함하는 UIView (SecondView)을로드하는 UIViewControler (RootController)이 있습니다. 문제는 UIViewnumberOfSectionsInTableViewnumberOfRowsInSection을 호출하지만 cellForRowAtIndexPath을 호출하지 않으며 테이블 뷰가 표시되지 않는다는 것입니다. RootViewController의 코드는 다음과 같습니다cellForRowAtIndexPath가 호출되지 않았지만 numberOfRowsInSection이라는 숫자가

SecondView *secondView = [[seconddView alloc] initWithFrame:CGRectMake(0, 60, self.view.bounds.size.width, self.view.bounds.size.height)]; 
[self.view addSubview:secondView]; 

그리고 SecondView의 코드는 다음과 같습니다

@interface SecondView() <UITableViewDelegate, UITableViewDataSource> 
@property (nonatomic,retain) UITableView *table; 
@end 

@implementation SecondView 
@synthesize table; 

- (id)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
    self.table = [[UITableView alloc] init]; 
    self.table.delegate = self; 
    self.table.dataSource = self; 
    [self addSubview:self.table]; 
    } 
return self; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.textLabel.text = @"Prova"; 
    return cell; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 5; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

당신은 나에게 문제를 찾는 데 도움이 될 수 있습니까? 고맙습니다.

+2

테이블 뷰 프레임이란 무엇입니까? – Wain

+1

이전 답변으로 해결되었지만 실제 설명은 다음과 같습니다. [http : // stackoverflow.COM/질문/7712325가/cellforrowatindexpath이--호출되지] [1] [1] : http://stackoverflow.com/questions/7712325/cellforrowatindexpath-not-called – GrandSteph

+0

정답이 여기에있다은 https ://stackoverflow.com/questions/7712325/cellforrowatindexpath-not-called – Ramis

답변

25

당신은 당신 만의 viewDidLoad가 호출 후의 ViewController의 관점 호출 할 수 있습니다 UITableView

+25

답변을 확대해야합니다. 한 줄의 답은별로 도움이되지 않습니다. –

+0

UITableViewController를 사용하고 있습니다. 같은 문제가 있습니다. 내 솔루션은 무엇입니까? –

1

의 프레임을 설정해야합니다. 당신은 당신의 경우에 당신의 init 메소드

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.table = [[UITableView alloc] initWithFrame:self.view.bounds]; 
    self.table.delegate = self; 
    self.table.dataSource = self; 
    [self addSubview:self.table]; 
} 

에서 self.view과 상호 작용할 수 없습니다, 당신은 (A는 위의 코드에서 제안처럼) 프레임으로의 tableview를 초기화하기 위해서 필요합니다. 그냥 의견을 조정하는 스토리 보드에있는 tableView에 적용된다 "누락 된 제약 조건을 추가"때 같은 이상한 문제가 나에게 발생 된 XCode6에서 당신의 ViewController

SecondView *secondView = [[seconddView alloc] initWithFrame:CGRectMake(0, 60,  self.view.bounds.size.width, self.view.bounds.size.height)]; 
[self.view addSubview:secondView]; 
6

에있는 viewDidLoad에서 코드를 추가해야합니다.

enter image description here

후 다음과 같은 방식으로 제약 조건을 적용 :

은 스토리 보드 최초의 명확한 제약에서이 문제를 해결하려면

enter image description here

+0

대단히 감사합니다! – fancy

9

이것은 또한 reloadData가 다른 불려 갔을 경우 일어날 수 실. 주 스레드에서 모든 UI 작업이 발생해야하므로 주 스레드에서 실행되는지 확인하십시오.

dispatch_async(dispatch_get_main_queue(),{ 
      self.myTableView.reloadData() 
     }); 
3

내 문제는 내가 내 위임 및 데이터 소스의 구현을하고 간단한 클래스 있다고했지만, 간단한 클래스의 수명이 너무 짧다. 내가 필요한

MyDataSourceClass* myClass = [[MyDataSourceClass alloc] initWithNSArray:someArray]; 
tableView.dataSource = myClass; 
tableView.delegate = myClass; 
[tableView reloadData]; 

// end of function, myClass goes out of scope, and apparently tableView has a weak reference to it 

을하고 있었다

위의 코드를 메모리에서 의사 것을

self.tableDataSource = [[MyDataSourceClass alloc] initWithNSArray:someArray]; 
tableView.dataSource = myClass; 
tableView.delegate = myClass; 
[tableView reloadData]; 
// now at the end of the function, tableDataSource is still alive, and the tableView will be able to query it. 

참고 일을해야합니다. "데이터 원본/위임자의 수명이 긴지 확인하십시오."라는 개념을 가져오고 붙여 넣기를 복사하지 마십시오. 프레임 등을 설정해야하는 다른 작업이 있기 때문입니다.

관련 문제