Refresh a UITableView on Button Click

Discussion in 'iOS Programming' started by raziiq, Aug 4, 2009.

  1. raziiq macrumors member

    Aug 26, 2008
    I have this code
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) 
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    		NSUInteger row = [indexPath row]; 
    		NSString *objectGotFromRow = [data objectAtIndex:row];
    		if ([objectGotFromRow hasPrefix:@"."])
    			UIImage *buttonUpImage = [UIImage imageNamed:@"Unhide.png"]; 
    			UIImage *buttonDownImage = [UIImage imageNamed:@"Unhide.png"]; 
    			UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    			button.frame = CGRectMake(0.0, 0.0, buttonUpImage.size.width, 
    			[button setBackgroundImage:buttonUpImage 
    			[button setBackgroundImage:buttonDownImage 
    			[button addTarget:self action:@selector(unHideButtonPressed:) 
    			cell.accessoryView = button;  
    	// Configure the cell.
    	NSUInteger row = [indexPath row]; 
    	NSString *objectGotFromRow = [data objectAtIndex:row];
        cell.textLabel.text = objectGotFromRow;
        return cell;
    And this code in my method

    - (IBAction)hideButtonPressed:(id)sender
    	UIButton *senderButton = (UIButton *)sender; 
        UITableViewCell *buttonCell = (UITableViewCell *)[senderButton superview]; 
        NSUInteger buttonRow = [[self.tableView indexPathForCell:buttonCell] row]; 
        NSString *buttonTitle = [data objectAtIndex:buttonRow]; 
    	NSString *path = @"/Some/Path/";
    	NSString *unhiddenPath = [path stringByAppendingString:buttonTitle];
    	NSString *pathForHiddenString = @"/Some/Path/";
    	NSString *hiddenPath = [pathForHiddenString stringByAppendingString:buttonTitle];
    	NSString *contents = [fileManager directoryContentsAtPath:path];
    	NSString *msg = [[NSString alloc] initWithFormat:@"File named %@ is now hidden",buttonTitle];
    	if ([fileManager moveItemAtPath:unhiddenPath toPath:hiddenPath error:nil])
    		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"File hidden !!!" message:msg delegate:nil cancelButtonTitle:@"Ya i know !!!" otherButtonTitles:nil];
    		[alert show];
    		[alert release];
    [B][self.tableView reloadData];[/B] 
    		[msg release];
    I want that whenever i press the Button i am creating in the Cell , the table should update and refresh or may be reload the data from the array. I have tried [self.tableView reloadData] but its not working for me. Where and how should i use the method to refresh my TableView on Button Click
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    If it's not working like that then try to performSelector:afterDelay: It would seem dicey to reloadData from inside a callback in a table cell since the table cell might be released.

Share This Page