我正在编写一个简单的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];
但是,如果课程号由行号确定,请执行以下操作:
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/