I have programmatically created a UITableView and I have also programmatically created UIButtons to be included with every cell / row. I have set these buttons so that if they are tapped, their button image changes. So when the user first sees the table view, the buttons are the color grey, but if the user taps one of the buttons, then that button will turn red. If they tap it again, it will turn back to grey.

I have also made it so that if a button has been pressed, and is currently using the red image, then it will pass it's cell's detailTextLabel property value into an NSMutableArray object.

Here's the problem though. For some reason, all of the buttons work fine except for the very first one. My UITableView has 2 sections. If I press the button for the first cell in the first section, for some reason it is grabbing the detailTextLabel property of the 2nd section's first cell.

Here is my code that controls the majority of the UITableView. This is where the cell's textLabels and detalTextLabels are set. The reason it is an if statement, is because if we find the person in our database then we perform the first code, else they weren't found in our database and we perform the second set of code:

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

static NSString *cellIdentifier = @"SettingsCell";

_cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

NSString* userName = nil;

if (indexPath.section == 0) {

userName = [self.potentiaFriendsInParseFirstNamesArray objectAtIndex:indexPath.row];

NSString *firstNameForTableView = [self.potentiaFriendsInParseFirstNamesArray objectAtIndex:indexPath.row];

NSString *userNameForTableView = [self.potentiaFriendsUsernameArray objectAtIndex:indexPath.row];

UIImage *addUserButtonImage = [UIImage imageNamed:@"SliderThumb-Normal-G"];
UIImage *addUserButtonImageHighlighted = [UIImage imageNamed:@"SliderThumb-Normal"];

_addUserButton.frame = CGRectMake(237, -10, 64, 64);

[_addUserButton setImage:addUserButtonImage forState:UIControlStateNormal];
[_addUserButton setImage:addUserButtonImageHighlighted forState:UIControlStateHighlighted];
[_addUserButton setImage:addUserButtonImageHighlighted forState:UIControlStateSelected];

[_addUserButton addTarget:self action:@selector(handleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];

_addUserButton.tag = indexPath.row;

[_cell.textLabel setText:firstNameForTableView];

[_cell.detailTextLabel setText:userNameForTableView];

[_cell.contentView addSubview:_addUserButton];

} else {

userName = [self.potentiaFriendsNotInParseFirstNamesArray objectAtIndex:indexPath.row];

NSString *firstNameForTableView = [self.potentiaFriendsNotInParseFirstNamesArray objectAtIndex:indexPath.row];

NSString *userNameForTableView = [self.potentiaFriendsPhoneNumberArray objectAtIndex:indexPath.row];

UIImage *addFriendButtonImage = [UIImage imageNamed:@"SliderThumb-Normal-G"];
UIImage *addFriendButtonImageHighlighted = [UIImage imageNamed:@"SliderThumb-Normal"];

UIButton *addFriendButton = [[UIButton alloc]init];

addFriendButton.frame = CGRectMake(237, -10, 64, 64);

[addFriendButton setImage:addFriendButtonImage forState:UIControlStateNormal];
[addFriendButton setImage:addFriendButtonImageHighlighted forState:UIControlStateHighlighted];     

[addFriendButton setImage:addFriendButtonImageHighlighted forState:UIControlStateSelected];

[addFriendButton addTarget:self action:@selector(handleTouchUpInsideForNonUsers:) forControlEvents:UIControlEventTouchUpInside];

addFriendButton.tag = indexPath.row;

[_cell.textLabel setText:firstNameForTableView];

[_cell.detailTextLabel setText:userNameForTableView];

[_cell.contentView addSubview:addFriendButton];

}
return _cell;
}

```

The below statements are method calls, one for the first part of the if statement, and another for the "else" part of the if statement. They handle the "touch" events and make the buttons change from the grey image to the red image when they are pressed:

```
[_addUserButton addTarget:self action:@selector(handleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];

```

and

```
[addFriendButton addTarget:self action:@selector(handleTouchUpInsideForNonUsers:) forControlEvents:UIControlEventTouchUpInside];

```

Here are the method implementations for these method calls. They are nearly identical except for the names of "handleTouchUpInside" and "handleTouchUpInsideForNonUsers", and then the first one's section is set to "0", and the second one's section is set to "1".

```
- (void)handleTouchUpInside:(UIButton *)sender {
sender.selected = !sender.selected;

UIButton *cellButton = (UIButton *)sender;

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:cellButton.tag inSection:0];

_cell = [_tableView cellForRowAtIndexPath:indexPath];

_cell.detailTextLabel.text = [_cell.detailTextLabel.text stringByReplacingOccurrencesOfString:@"    " withString:@""];

if(sender.state == 5) {

[_usersToAddToFriendsList addObject:_cell.detailTextLabel.text];

} else {

[_usersToAddToFriendsList removeObject:_cell.detailTextLabel.text];

}
}

- (void)handleTouchUpInsideForNonUsers:(UIButton *)sender {
sender.selected = !sender.selected;

UIButton *cellButton = (UIButton *)sender;

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:cellButton.tag inSection:1];

_cell = [_tableView cellForRowAtIndexPath:indexPath];

if(sender.state == 5) {

[_usersToInviteToApp addObject:_cell.detailTextLabel.text];

} else {

[_usersToInviteToApp removeObject:_cell.detailTextLabel.text];

}   
}

```

Another important part of the first code that I showed you, is how I am setting the UIButton tag's in both parts of the if statement. I have a feeling these may be part of the problem:

`addUserButton.tag = indexPath.row;` and `addFriendButton.tag = indexPath.row;`

These are used to add a tag to each button. I added these in because I asked a question on SO yesterday when I was first trying to set all of this up, and the button tags were part of the solution someone gave me. Here is the solution/answer I ended up using: <https://stackoverflow.com/a/22030199/3344977>

Last but not least, here are the final 3 method implementations that help create my UITableView and all of it's aspects:

```
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2 ;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if(section == 0)
    return [self.potentiaFriendsInParseFirstNamesArray count];
if(section == 1)
    return [self.potentiaFriendsNotInParseFirstNamesArray count];

else return 0;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

if(section == 0)
    return @"Friends Using This App";
if(section == 1)
    return @"Send to Contact";

else return @"nil";
}

```

UPDATE: I added an NSLog for the cell button's tag to the method implementations for "handleTouchUpInside" and "handleTouchUpInsideForNonUsers". All of the buttons print the correct button tag to the console. So for the first button in the first section, it does print the correct button tag number of "0". So I think the real problem is that for some reason, the very first button in the first section, is being assigned to the wrong section. 

I think it is being assigned to the 2nd section instead of the first.

UPDATE 2: Right now, when you press one of the buttons, it is supposed to access the property "detailTextLabel.text". All of the buttons do this fine except the first one in the first section.

For this first button however, even if I change the property to "textLabel.text", it still ALWAYS prints the "detailTextLabel.text" of the 2nd section's first cell.

UPDATE 3: For the first part of the if statement, which creates the first section of the table view, instead of using both "SliderThumb-Normal" and "SliderThumb-Normal-G" for the images I changed them both to just "SliderThumb-Normal". Sure enough, the 2nd cell in the first section is correct and is using the image of "SliderThumb-Normal", but the first cell in the first section is using the image of "SliderThumb-Normal-G".

I think when the if statement is running, the first cell must not have a section of "0", because it is being run through the "else" part of the if code. I just can't figure out why.

EDIT FOR TIMOTHY: I put a lot of them inside the interface area of my main file and also my header file.

Here is my header file:

```
@interface AddFriendsViewController : UIViewController <UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *friendsOnApp;
@property (nonatomic, retain) NSArray *objects;
@property (nonatomic, retain) NSArray *objectsFinal;
@property (nonatomic, retain) UITableView *addFriendsTableView;
@property (nonatomic, retain) PotentialFriend *potentialFriend;
@property (nonatomic, retain) PotentialFriend *potentialFriendForTableView;
@property (nonatomic, retain) NSString *potentialFriendz;
@property (nonatomic, strong) NSString *imageFile;

@property (nonatomic, strong) UITableViewCell *cell;
@property (nonatomic, strong) UITableViewCell *cellz;

@property (nonatomic, strong) UIButton *addUserButton;
@property (nonatomic, strong) UIButton *addFriendButton;

@end

```

Here is my main file's interface area:

```
@interface AddFriendsViewController ()

@property (nonatomic, strong) NSMutableArray* contactList;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *potentiaFriendsInParseFirstNamesArray;
@property (strong, nonatomic) NSMutableArray *potentiaFriendsUsernameArray;

@property (strong, nonatomic) NSMutableArray *potentiaFriendsNotInParseFirstNamesArray;
@property (strong, nonatomic) NSMutableArray *potentiaFriendsPhoneNumberArray;

@property (strong, nonatomic) NSMutableArray *removalPhoneNumberArray;

@property (strong, nonatomic) NSMutableArray *usersToAddToFriendsList;
@property (strong, nonatomic) NSMutableArray *usersToInviteToApp;

@end

```

And then I initialize these in the viewDidLoad method implementation of the main file:

```
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

//This sets the UITableView's data source as itself.

self.tableView.dataSource = self;

self.potentiaFriendsInParseFirstNamesArray = [[NSMutableArray alloc] init];
self.potentiaFriendsUsernameArray = [[NSMutableArray alloc]init];

self.potentiaFriendsNotInParseFirstNamesArray = [[NSMutableArray alloc]init];
self.potentiaFriendsPhoneNumberArray = [[NSMutableArray alloc]init];

self.removalPhoneNumberArray = [[NSMutableArray alloc]init];

self.usersToAddToFriendsList = [[NSMutableArray alloc]init];
self.usersToInviteToApp = [[NSMutableArray alloc]init];

self.addUserButton = [[UIButton alloc]init];
self.addFriendButton = [[UIButton alloc]init];

self.cell = [[UITableViewCell alloc]init];

```