2013-10-13 33 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變量之前的某處。