2013-10-13 34 views
1

我想通过将其替换为从警报视图返回的文本来替换数组中的对象。从表视图中检索索引路径

到目前为止,我有:

int selected = indexPath.row; 

和我alertview委托方法。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    if (alertView.tag == 1) { 
     [myMutableArray replaceObjectAtIndex:selected withObject:[[alertView textFieldAtIndex:0] text]]; 
     [_tableView reloadData]; 
    } 
} 

我不断收到的

不相容整数指针转换从 'NSInteger的'(亦称 '长')分配给 'NSInteger的*'(又名 '长*')

+0

这是你的全部代码吗?你在哪里初始化NSMutableArray?如果可能,请发布所有代码。 – wigging

+0

如果它帮助你找出你的问题,请标记一个答案。 – wigging

回答

0

如果没有错误知道你的其他代码是什么样的,你可以在你的ViewController.m文件中试试这个。它设置标签的文本,并在按下“确定”按钮时,将警报视图中的文本替换为可变数组中的对象。

#import "ViewController.h" 

@interface ViewController() <UIAlertViewDelegate> 

@property (strong,nonatomic) NSMutableArray *mutArray; 
@property (weak,nonatomic) IBOutlet UILabel *label; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSArray *array = @[@"item1",@"item2",@"item3"]; 
    self.mutArray = [[NSMutableArray alloc] init]; 
    self.mutArray = [array mutableCopy]; 
} 

- (IBAction)showAlert:(id)sender { 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My Alert Example" 
                message:@"message here" 
                delegate:self 
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"OK",nil]; 

    alert.alertViewStyle = UIAlertViewStylePlainTextInput; 

    [alert show]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 

    int selected = 1; 

    if (buttonIndex != alertView.cancelButtonIndex) { 
     NSLog(@"ok button"); 
     UITextField *textField = [alertView textFieldAtIndex:0]; 
     self.label.text = textField.text; 
     [self.mutArray replaceObjectAtIndex:selected withObject:textField.text]; 
     NSLog(@"mutArray is %@",self.mutArray); 
    } else { 
     NSLog(@"cancel button"); 
    } 
} 

@end 

因为它看起来像你使用一个UITableView,你的情况,你将有int selected = indexPath.row而不是int selected = 1

2

您看到的错误来自于您将*放在NSInteger变量之前的某处。