This question already has answers here:
what if when two UITableView in one ViewController and one with custom cell refrence and another is a simple
(6个答案)
5年前关闭。
随附的代码返回错误:
码:
我知道问题特别在“返回”时出现,但是如何消除错误?
或“其他”情况:
注意:重用标识符中也有错字:
(6个答案)
5年前关闭。
随附的代码返回错误:
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)
我知道问题特别在“返回”时出现,但是如何消除错误?
最佳答案
您要考虑indexPath.row == 0
和indexPath.row == 1
的情况,但是编译器会说:如果行不是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; //<--Add this line
}
或“其他”情况:
- (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;
}
else //<--Add this clause
{
OtherCustomCell *oCustomCell = [tableView dequeueReusableCellWithIdentifier:@"otherCustomCell" forIndexPath:indexPath];
if (oCustomCell == nil)
{
oCustomCell = [[OtherCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"otherCustomCell"];
}
return sCustomCell;
}
}
注意:重用标识符中也有错字:
"secondCustomCell"
与"SecondCustomCell"
不同关于ios - 控制可能会达到非无效功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27742523/
10-14 23:41