Fabre
Fabre

Reputation: 57

Unable to update uitableview rows

I am trying to update selected rows of UITableView from UIAlertView inside didSelectRowAtIndexPath. But I am facing a problem. Each time I try to update any row, it's only updates the first row irrespective of selection of row.
My code is:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *selectedrow = nil;

if (searching) {
    slCell=indexPath.row;
    selectedrow = [copyListOfItems objectAtIndex:indexPath.row];


UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter the Number of Quantity" message:@""
                                               delegate:self cancelButtonTitle:nil  otherButtonTitles:@"OK", nil];

[alert addTextFieldWithValue:@"" label:@"Number of Item"];  
textfieldQty = [alert textFieldAtIndex:0];
textfieldQty.keyboardType = UIKeyboardTypeNumberPad;
textfieldQty.keyboardAppearance = UIKeyboardAppearanceAlert;
textfieldQty.autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
[alert release];
}
else {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter the Number of Quantity" message:@""
                                                   delegate:self cancelButtonTitle:nil  otherButtonTitles:@"OK", nil];

    [alert addTextFieldWithValue:@"" label:@"Number of Item"];  
    textfieldQty = [alert textFieldAtIndex:0];
    textfieldQty.keyboardType = UIKeyboardTypeNumberPad;
    textfieldQty.keyboardAppearance = UIKeyboardAppearanceAlert;
    textfieldQty.autocorrectionType = UITextAutocorrectionTypeNo;
    [alert show];
    [alert release];
        }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *b=nil;
if (buttonIndex == 0) {
    if (searching) {
        if([textfieldQty.text isEqualToString:b]) {
            UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Enter plz" message:@""
                                                           delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK"];
            [alert2 show];
            [alert2 release];
        }


    NSString *newqty = [[NSString alloc] initWithFormat:@"%@",textfieldQty.text];
    DMSAppDelegate *d= (DMSAppDelegate *)[[UIApplication sharedApplication] delegate];

    [d->tableDataQt replaceObjectAtIndex:slCell withObject:(id)newqty];

    NSLog(@"tb%@",copyListOfQty);

    }
    else {

        NSString *newqty = [[NSString alloc] initWithFormat:@"%@",textfieldQty.text];
        DMSAppDelegate *d= (DMSAppDelegate *)[[UIApplication sharedApplication] delegate];
        [d->tableDataQt replaceObjectAtIndex:slCell withObject:(id)newqty];
        [self.tablevieww reloadData];
    }

}   
}

Upvotes: 0

Views: 246

Answers (1)

BornCoder
BornCoder

Reputation: 1066

Please follow below steps to achieve what you want.

  1. While creating UIAlertview in (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method, set alerview's tag as indexpath's row.
  2. In delegate method -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex, you try to access alertView's tag and pass that tag to the method call [d->tableDataQt replaceObjectAtIndex:[alertView tag] withObject:(id)newqty];
  3. In AppDelegate class, you create one more method called -(void)replaceObjectAtIndex:(NSInteger>inIndex withObject:(id)inObject
  4. In above newly created method try to access UITableViewCell from UITableView object with method call UITableViewCell *myCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:inIndex inSection:0];
  5. This way, you will get the object of UITableViewCell and alter the value of that and reload the table.

I hope this will help you. Please do not forget to put right mark if this resolves your problem. :)

Thanks.

Upvotes: 1

Related Questions