Ios 如何返回两个唯一值中的一个并避免;控制可能达到非无效功能的末尾“;
我理解返回值的价值,毕竟如果/for/while方法是这样运行的:Ios 如何返回两个唯一值中的一个并避免;控制可能达到非无效功能的末尾“;,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我理解返回值的价值,毕竟如果/for/while方法是这样运行的: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { int rowHeight = 0; if (tableView == secondTable) { rowHeight = 62; } if
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
int rowHeight = 0;
if (tableView == secondTable) {
rowHeight = 62;
}
if (tableView == thirdTable) {
rowHeight = 72;
}
return rowHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *result = nil;
if (tableView == secondTable) {
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell2 *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[CustomCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
result = cell;
} else {
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell3 *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[CustomCell3 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
result = cell;
}
return result;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (tableView == secondTable) {
static NSString *MyIdentifier = @"MyIdentifier";
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = (UITableViewCell *)[[CustomCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
} else if (tableView == thirdTable) {
static NSString *MyIdentifier = @"MyIdentifier";
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = (UITableViewCell *)[[CustomCell3 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
} else {
// init an empty cell of some kind
// cell = ...
}
return cell;
}
但是我可以声明哪种变量能够承载不同的结果。。。在本例中,UITableViewCell有两个不同的自定义类:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// some variable here?
if (tableView == secondTable) {
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell2 *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[CustomCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
return cell; //don't return yet
}
if (tableView == thirdTable) {
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell3 *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[CustomCell3 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
return cell; //don't return yet
}
// Ideally would return a UITableViewCell here to avoid "control may reach end of non-void function"
}
如果您知道只有两个表视图,请将第二个
If
替换为else
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == secondTable) {
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell2 *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[CustomCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
return cell;
} else {
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell3 *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[CustomCell3 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
return cell;
}
}
通常,如果只有一条语句是真的,则避免使用多条if
语句。根据需要使用if/else if/else
if (condA) {
} else if (condB) {
} else {
}
另一种选择是使用如下内容:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
int rowHeight = 0;
if (tableView == secondTable) {
rowHeight = 62;
}
if (tableView == thirdTable) {
rowHeight = 72;
}
return rowHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *result = nil;
if (tableView == secondTable) {
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell2 *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[CustomCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
result = cell;
} else {
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell3 *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[CustomCell3 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
result = cell;
}
return result;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (tableView == secondTable) {
static NSString *MyIdentifier = @"MyIdentifier";
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = (UITableViewCell *)[[CustomCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
} else if (tableView == thirdTable) {
static NSString *MyIdentifier = @"MyIdentifier";
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = (UITableViewCell *)[[CustomCell3 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
} else {
// init an empty cell of some kind
// cell = ...
}
return cell;
}
但是,与前面的建议相比,这没有什么好处,除了在方法的末尾有一个
返回值。如果您知道只有两个表视图,请将第二个If
替换为else
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == secondTable) {
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell2 *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[CustomCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
return cell;
} else {
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell3 *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[CustomCell3 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
return cell;
}
}
通常,如果只有一条语句是真的,则避免使用多条if
语句。根据需要使用if/else if/else
if (condA) {
} else if (condB) {
} else {
}
另一种选择是使用如下内容:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
int rowHeight = 0;
if (tableView == secondTable) {
rowHeight = 62;
}
if (tableView == thirdTable) {
rowHeight = 72;
}
return rowHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *result = nil;
if (tableView == secondTable) {
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell2 *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[CustomCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
result = cell;
} else {
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell3 *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[CustomCell3 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
result = cell;
}
return result;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (tableView == secondTable) {
static NSString *MyIdentifier = @"MyIdentifier";
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = (UITableViewCell *)[[CustomCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
} else if (tableView == thirdTable) {
static NSString *MyIdentifier = @"MyIdentifier";
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = (UITableViewCell *)[[CustomCell3 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
} else {
// init an empty cell of some kind
// cell = ...
}
return cell;
}
但是与前面的建议相比,这没有什么好处,除了在方法的末尾有一个返回值。不能保证表视图总是第二个表
或第三个表
,因此在其他情况下最好返回一个空单元格。有一个空牢房总比一无所有而崩溃要好
应该是这样的:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
int rowHeight = 0;
if (tableView == secondTable) {
rowHeight = 62;
}
if (tableView == thirdTable) {
rowHeight = 72;
}
return rowHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *result = nil;
if (tableView == secondTable) {
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell2 *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[CustomCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
result = cell;
} else {
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell3 *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[CustomCell3 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
result = cell;
}
return result;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (tableView == secondTable) {
static NSString *MyIdentifier = @"MyIdentifier";
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = (UITableViewCell *)[[CustomCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
} else if (tableView == thirdTable) {
static NSString *MyIdentifier = @"MyIdentifier";
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = (UITableViewCell *)[[CustomCell3 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
} else {
// init an empty cell of some kind
// cell = ...
}
return cell;
}
使用此方法将使用从自定义类向下转换到超级类UITableViewCell的方法
请注意,必须显式强制转换以避免编译时警告
如果要添加更多自定义单元格,请使用开关替换If语句。不能保证tableView
始终是secondTable
或thirdTable
,因此在其他情况下,最好返回空单元格。有一个空牢房总比一无所有而崩溃要好
应该是这样的:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
int rowHeight = 0;
if (tableView == secondTable) {
rowHeight = 62;
}
if (tableView == thirdTable) {
rowHeight = 72;
}
return rowHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *result = nil;
if (tableView == secondTable) {
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell2 *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[CustomCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
result = cell;
} else {
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell3 *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[CustomCell3 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
result = cell;
}
return result;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (tableView == secondTable) {
static NSString *MyIdentifier = @"MyIdentifier";
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = (UITableViewCell *)[[CustomCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
} else if (tableView == thirdTable) {
static NSString *MyIdentifier = @"MyIdentifier";
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = (UITableViewCell *)[[CustomCell3 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
} else {
// init an empty cell of some kind
// cell = ...
}
return cell;
}
使用此方法将使用从自定义类向下转换到超级类UITableViewCell的方法
请注意,必须显式强制转换以避免编译时警告
如果您打算添加更多自定义单元格,请使用开关替换If语句。由于两个单元格都是UITableViewCell的子类,您只需声明一个UITableViewCell变量,然后将单元格
分配给该变量,其中包含返回语句,然后在末尾返回该变量。因为两个单元格都是UiTabkeViewCell的子类你可以声明一个UITableViewCell变量,然后将cell
赋值给这个变量,在这里你有返回语句,然后在末尾返回这个变量。为了帮助将来理解,我会添加它,你可以这样做:if(condA){}else/*condB*/{}
在另一个例子中添加注释。这就是我用两个布尔值做if/elseif/elseif/else时所做的,例如(if(A&&B)elseif(!A&&B)elseif(A&&B)else/*!A&&B)else/*!A&&B*/我将添加这一点以帮助将来理解,您可以这样做:if(condA){}else/*condB*/{}
将注释放在另一种情况下。这就是我在使用两个布尔值(例如if(A&&B)elseif(!A&&B)elseif(A&&B)进行if/elseif/else时所做的操作else/*!A&&!B*/我个人更喜欢这种方法,即只有一个返回,并且它位于函数的末尾。我通常将原始变量初始化为nil,因此如果我遗漏了一些内容,则会出现一致的故障模式。我个人更喜欢这种方法,即只有一个返回,并且它位于函数的末尾。我通常但是,将原始变量初始化为nil,这样如果我遗漏了什么,就会出现一致的故障模式。