剛剛在開發(fā)的時候碰到這樣一個問題馏颂,閃退,lldb提示信息:[UITableView _configureCellForDisplay:forIndexPath:]棋傍,之前遇到過這個問題救拉,忘了怎么解決了,然后又是一堆搜索瘫拣。結果發(fā)現(xiàn)是因為
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法返回的cell為nil的問題
參考這里
問題代碼如下亿絮,這是會發(fā)生閃退的代碼
- (UITableView *)canJoinTableView
{
if (!_canJoinTableView) {
_canJoinTableView = [[UITableView alloc] init];
[_canJoinTableView registerClass:[WLInvestAssistantUnJoinedCell class] forCellReuseIdentifier:kInvestAssistantCellUnJoined];
[_canJoinTableView registerClass:[WLInvestAssistantJoinedCell class] forCellReuseIdentifier:kInvestAssistantCellJoined];
_canJoinTableView.delegate = self;
_canJoinTableView.dataSource = self;
_canJoinTableView.backgroundColor = WLColorBgGray_VC;
_canJoinTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
return _canJoinTableView;
}
修復后正確代碼如下:
- (UITableView *)canJoinTableView
{
if (!_canJoinTableView) {
_canJoinTableView = [[UITableView alloc] init];
_canJoinTableView.delegate = self;
_canJoinTableView.dataSource = self;
_canJoinTableView.backgroundColor = WLColorBgGray_VC;
_canJoinTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[_canJoinTableView registerClass:[WLInvestAssistantUnJoinedCell class] forCellReuseIdentifier:kInvestAssistantCellUnJoined];
[_canJoinTableView registerClass:[WLInvestAssistantJoinedCell class] forCellReuseIdentifier:kInvestAssistantCellJoined];
}
return _canJoinTableView;
}
細心的讀者可能發(fā)現(xiàn)只是調整了設置delegate、dataSource和 registerClass:的位置而已麸拄,調整代碼順序之后閃退現(xiàn)象消失派昧,返回的cell不為nil,個人理解是registerClass:的內部實現(xiàn)依賴于設置delegate拢切、dataSource的代理方法蒂萎,所以必須按照正確順序實現(xiàn)。