0

I have an array of user displayed in a table view when pushing the send button the cell dosen't selected right object. It can be quit random:). How do i make send the object displayed on the selected cell?

This is how i send my message

- (void)sendMessage:(id)sender {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        PFObject *object = [self.objects objectAtIndex:indexPath.row];
        self.SendToUsername = [object objectForKey:@"username"];     
        self.SendToName = [object objectForKey:@"name"];
}

And this is my cell, where the send button is located.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
    static NSString *simpleTableIdentifier = @"LampCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    UIButton *sendbutton = (UIButton*) [cell viewWithTag:105];
    [sendbutton addTarget:self action:@selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

enter image description here

user3615707
  • 89
  • 1
  • 9

3 Answers3

0

It is quite easy. Tableview itself provide method.

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

     variable=[array objectAtIndex:indexPath.row];
}

In "Variable" you have value which is selected. If your array is accessible in whole controller then you can save "indexPath.row" and use on click event of send button to fetch selected record.

nadim
  • 776
  • 1
  • 12
  • 26
  • I updated my question as you can see I'm using a button with in my cell. – user3615707 Sep 05 '14 at 12:23
  • is it neccessary to add button? If you are not used button then and also on select of row it will call method where you can write send message code, or even you can call send message method on selection of table cell row. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ variable=[array objectAtIndex:indexPath.row]; // here call method what ever you want } – nadim Sep 05 '14 at 13:09
  • Thanks i updated my question with an image now. Maybe it's more understandable what I want to do? Thanks again – user3615707 Sep 07 '14 at 14:51
0

tableView indexPathForSelectedCell will not give you the index path you are expecting in the action method of a button that is in a cell. The cell wasn't selected - you touched the button.

To get the index path of the row for that button, there are a couple of different methods.

My preferred method is to traverse the view hierarchy to find the cell that contains the button, and use that to get the index path. See this question for more info:

Button in custom cell in dynamic table: how to know which row in action method?

My answer from this question is as follows. You could add these two methods to a category on UITableViewController, or you could just add them to your view controller, if you like.

- (NSIndexPath *)indexPathForCellSubview:(UIView *)subview
{
    if (subview) {
        UITableViewCell *cell = [self tableViewCellForCellSubview:subview];
        return [self.tableView indexPathForCell:cell];
    }
    return nil;
}

- (UITableViewCell *)tableViewCellForCellSubview:(UIView *)subview
{
    if (subview) {
        UIView *superView = subview.superview;
        while (true) {
            if (superView) {
                if ([superView isKindOfClass:[UITableViewCell class]]) {
                    return (UITableViewCell *)superView;
                }
                superView = [superView superview];
            } else {
                return nil;
            }
        }
    } else {
        return nil;
    }
}

You could then get the index path in your button action method like so:

NSIndexPath *indexPath = [self indexPathForCellSubview:sender];
Community
  • 1
  • 1
JoeFryer
  • 2,751
  • 1
  • 18
  • 23
  • Thanks a lot but not working yet. Could the problem be that the button is next to an image? Could another solution be to use a static table view with different cells or could that also mess up when you scroll to the next user in the end? I updated my question with an image so maybe it helps? – user3615707 Sep 07 '14 at 14:47
0

You don't need to set the tag for buttons to get the indexpath. You can simply get it using this piece of code:

- (void)sendMessage:(id)sender {
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *clickedButtonIndexPath = [self.tableView indexPathForCell:clickedCell];
        PFObject *object = [self.objects objectAtIndex:indexPath.row];
        self.SendToUsername = [object objectForKey:@"username"];     
        self.SendToName = [object objectForKey:@"name"];
}
Ajay
  • 1,622
  • 20
  • 36
  • Thanks. Hmm. Still not working. Everything is in the same cell in a dynamic Table View. See the image in the updated question. It's button called button on the image that want work – user3615707 Sep 07 '14 at 14:44