附加代码返回错误:
Control may reach end non-void function
代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
FirstCustomCell *fCustomCell = [tableView dequeueReusableCellWithIdentifier:@"firstCustomCell" forIndexPath:indexPath];
if (fCustomCell == nil) {
fCustomCell = [[FirstCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"firstCustomCell"];
}
return fCustomCell;
}
else if (indexPath.row == 1) {
SecondCustomCell *sCustomCell = [tableView dequeueReusableCellWithIdentifier:@"secondCustomCell" forIndexPath:indexPath];
if (sCustomCell == nil) {
sCustomCell = [[SecondCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SecondCustomCell"];
}
return sCustomCell;
}
} //<-- Control may reach non-void function (I precise that's the end of the cellForRowAtIndexPath method)
我知道这个问题是特别在“返回”但是如何消除错误?
答案 0 :(得分:1)
添加return nil;
行。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
FirstCustomCell *fCustomCell = [tableView dequeueReusableCellWithIdentifier:@"firstCustomCell" forIndexPath:indexPath];
if (fCustomCell == nil) {
fCustomCell = [[FirstCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"firstCustomCell"];
}
return fCustomCell;
}
else if (indexPath.row == 1) {
SecondCustomCell *sCustomCell = [tableView dequeueReusableCellWithIdentifier:@"secondCustomCell" forIndexPath:indexPath];
if (sCustomCell == nil) {
sCustomCell = [[SecondCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SecondCustomCell"];
}
return sCustomCell;
}
return nil;
}
我可能会补充一点,您需要确保fCustomCell
和sCustomCell
不是{!=
)nil
。
if (!fCustomCell) {
fCustomCell = [UITableViewCell alloc] initWithStyle:/*aStyle*/ reuseIdentifier:/*identifier*/];
}
标识符可以是在方法开头定义的静态NSString,如下所示:static NSString *identifier = @"cell";
查看一些教程。