Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Fabio Floris
Fabio Floris
526 Points

TableView and Button in customCell not working

then I have a problem with Parse.com ... Using parse to manage the data of my app.

I have a tableview (custom cell) with a list of all subscribers to the app ... When a user displays the list of all registered users, next to each name has the ability to request a friendship (like facebook) through the use of a button that sends or drops the request.

to store the state of the button in TableView (formerly effettuavo when scrolling the tableview not memorize the state of the button) and now I have created an array with this I manage this phase .. below is the code I used in cellForowAtIndexPath

   -(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {

static NSString *CellIdentifier = @"cell";
NPCustomFindUserCell *cell = [tableViewFindUser dequeueReusableCellWithIdentifier:CellIdentifier];
cell.delegate = self;

if ([self.cellsCurrentlyEditing containsObject:indexPath]) {
    [cell openCell];
}


UIButton *myButton = (UIButton *)[cell.contentView viewWithTag:100];
UIImage *imageFriendAdd = [UIImage imageNamed:@"On_userAddFriend@2x"];

if (myButton == nil) {
    myButton = [UIButton buttonWithType:UIButtonTypeSystem];
    myButton.frame = CGRectMake(264.0, 30.0, 30.0, 30.0);
    [myButton setBackgroundImage:imageFriendAdd forState:UIControlStateNormal];
    myButton.tag = 100;
    myButton.tintColor = [UIColor redColor];
    [myButton addTarget:self action:@selector(tapped:andEvent:) forControlEvents:UIControlEventTouchUpInside];
    [cell.myContentView addSubview:myButton];


    NSLog(@" BOTTONE ADD CREATO !!!");

}  else {


    NSLog(@" BOTTONE ADD GIA'____CREATO !!!");

}

//DETECT BUTTON CLICKED IN TABLEVIEW/////////

if ([self.buttonPressedSelectedRowArray [indexPath.row] boolValue]) {
    UIImage *imageFriendAddOn = [UIImage imageNamed:@"On_userAddFriend@2x"];
    [myButton setBackgroundImage:imageFriendAddOn forState:UIControlStateNormal];
}

else {
    UIImage *imageFriendAddOff = [UIImage imageNamed:@"Off_userAddFriend@2x"];
    [myButton setBackgroundImage:imageFriendAddOff forState:UIControlStateNormal];
}

return cell; } To manage the request in Pending I created a custom class in a query and parse call active requests for the current user by putting in another array.

All this is handled with a Boolean

-(BOOL)Is_InAttesa:(PFUser *)user_inattesa {

for (PFUser *userInAttesa in amiciInAttesaMutableArray) {

if ([[[userInAttesa objectForKey:NPFriend_AUser]objectId] isEqualToString:user_inattesa.objectId]) {
        return YES;
    }
}
return NO;

} Now in cellForRowAtIndexPath should implement the boolean method to display the list of users which request are pending and use this:

PFUser *user = [self.userArray objectAtIndex:indexPath.row];

if ([self Is_InAttesa:user]) {

    UIImage *imageFriendAddOn = [UIImage imageNamed:@"On_userAddFriend@2x"];
    [myButton setBackgroundImage:imageFriendAddOn forState:UIControlStateNormal];

}

else {

    UIImage *imageFriendAddOff = [UIImage imageNamed:@"Off_userAddFriend@2x"];
    [myButton setBackgroundImage:imageFriendAddOff forState:UIControlStateNormal];

}

The problem is that the list is updated with the pending requests but when I go to create new applications (again with the button in custom cell) it loses its state when I scroll the tableview, in a nutshell are back to the starting point ...

How come these two operations are in conflict with each other? because I can not maintain the state of the button and contemporanemanete display (via query) requests previously sent? Can you help me in this regard?

Sorry if I was not very clear ... I remain available for any inquiry ...

Hey Fabio, do you have a git repository that I can check out? if not a .zip works fine too, this is just so i can use some xcode tool to help pin down what's messing up.

b.t.w. I've already learned a fair bit of italian from your code ;)

2 Answers

Fabio Floris
Fabio Floris
526 Points

Hi kai my problem is this:

if I select the cell 0 in normal mode ... even in the search mode becomes selected ... the problem ':

the user selects the cell containing FABIO

the tableview is thus: normal cell: Fabio

user filters the data to find WILL search mode cell = WILL

WILL ​​he also has the yellow button but it should not be like this:

Whether in search mode in normal mode only Fabio must have the yellow button because the user has clicked on Fabio and not on WILL

The File.m http://pastie.org/9591396

The File.h http://pastie.org/9591403