简体   繁体   中英

Not selecting the correct object in UI table View Cell

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;
}

在此处输入图片说明

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.

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];

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"];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM