我正在编写一个简单的 tableview 应用程序,当点击该行时,会出现一个警告。
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *rowValue =self.greekLetters[indexPath.row];
// NSString *message=[[NSString alloc] initWithFormat:@"You selected%@!",rowValue];
if (indexPath.row==0) {
NSString *message=[[NSString alloc] initWithFormat:@"This is course aaaaa",rowValue];
}
if (indexPath.row==1) {
NSString *message=[[NSString alloc] initWithFormat:@"This is course aaaaa"];
}
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Course introduction" message:message delegate:nil cancelButtonTitle:@"Return to courses list" otherButtonTitles:nil, nil];
[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
但是,Xcode 不喜欢我这里的 if 语句,而下面的语句有效。
NSString *message=[[NSString alloc] initWithFormat:@"You selected%@!",rowValue];
谁能给我一个如何使用这里消息的例子吗?
最佳答案
这是作用域的问题。您需要在 if 语句之前声明消息:
NSString *message;
if (indexPath.row==0) {
message=[[NSString alloc] initWithFormat:@"This is course aaaaa",rowValue];
}
else if (indexPath.row==1) {
message=[[NSString alloc] initWithFormat:@"This is course aaaaa"];
}
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Course introduction" message:message delegate:nil cancelButtonTitle:@"Return to courses list" otherButtonTitles:nil];
[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
但是,如果类(class)编号由行号确定:
NSString *rowValue =self.greekLetters[indexPath.row];
NSString *message = [[NSString alloc] initWithFormat:@"This is course %@",rowValue];
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Course introduction" message:message delegate:nil cancelButtonTitle:@"Return to courses list" otherButtonTitles:nil];
[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
关于ios - Tableview显示警报错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27882063/