Jordan Newton
Jordan Newton

Reputation: 35

Retrieving index path from table view

i am trying to replace an object at in an array by replacing it with the text returned from an alert view.

so far i have:

int selected = indexPath.row;

and for my alertview delegate method.

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

i keep getting an error of

Incompatible integer to pointer conversion assigning to 'NSInteger *' (aka 'long *') from 'NSInteger' (aka 'long')

Upvotes: 1

Views: 415

Answers (2)

Mundi
Mundi

Reputation: 80271

The error you see comes from the fact that you put a * somewhere before the NSInteger variable.

Upvotes: 2

wigging
wigging

Reputation: 9190

Without knowing what the rest of your code looks like, you could try this in your ViewController.m file. It sets the text of a label and replaces an object in a mutable array with the text from the alert view when the "OK" button is pressed.

#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

Since it looks like you're using a UITableView, in your case you would have int selected = indexPath.row instead of int selected = 1.

Upvotes: 0

Related Questions