views:

47

answers:

1

Hi

after entering with a new item, in the delegate method from UIAlertview, I calling reloadData from Tableview and it is not being updated:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
                                           forRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    {
        if (indexPath.row > 0) 
        {
            [datController RemoveData:indexPath.row-1];
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                         withRowAnimation:UITableViewRowAnimationFade];
        }

    } 
    else if(editingStyle == UITableViewCellEditingStyleInsert)
    {
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Enter the item." message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil];    

        fldItem = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
        [fldItem setBackgroundColor:[UIColor whiteColor]];

        [myAlertView addSubview:fldItem];

        [tableView reloadData]; 
        [myAlertView show];
        [myAlertView release];          
    }       

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{
    if(buttonIndex == 0){}          
    else 
    {   
        [datController AddData:fldItem.text];
        [self.tableView reloadData];        
    }
}

Note that reloadData in the method commitEditingStyle, I put in for debugging purposes only, and this place it's working.

Method cellForRowAtIndexPath which redraws the tableview, is not fired after execute reloadData:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    //Try to get rusable (rusable) cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
    if (cell == nil) 
    {
        //If not possible create a new cell
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:@"CellIdentifier"] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    // Get the string to display and set the value in the cell
    if(indexPath.row == 0)
    {
        cell.textLabel.text  = @"Add items...";
    }
    else
    {
        cell.textLabel.text = [datController ListData:indexPath.row-1];
    }
    return cell;
}   

Thanks

Smith

A: 

Not sure what would have gone wrong to end up in the situation, but are you sure that self.view is not nil?

In the place the reloadData call is working you're using the tableView reference passed to you and not self.view, but the call that isn't working is using self.view. With just the code above that's the only obvious thing I noticed so far.

imaginaryboy
I'm not sure that self.tableview is not nil. Actually, I have doubts about should I call the reloadData from Tableview, in the delegate method cellForRowAtIndexPath. Any suggestion? Thank you.
musicnt
You should absolutely not call `reloadData` from `cellForRowAtIndexPath`.
imaginaryboy
Sorry, I meant the method clickedButtonAtIndex. How should I call in this method.
musicnt
Whether you should or should not call it from clickedButtonAtIndex: is determined by the design of your software, and I can't really say whether it's a good idea or not. I can say that you clearly wrote that method expecting `self.tableView` was not nil, so if it is, you should figure out why.
imaginaryboy