Tableview 显示警告错误

Tableview display alert error

我正在编写一个简单的 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= [[NSString alloc] initWithFormat:@"This is course aaaaa %@",rowValue];

顺便说一句,根本不需要做[[NSString alloc] init],只需要使用[NSString stringWithFormat:...]

这是范围问题。您需要在 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];