简体   繁体   中英

iOS UIButton on cell with target action, cell not updated in (id)sender

I have on custom cell UIButton with UIImage content, with target action selector pushToImageDetailViewController: which push view to another detail view of image. I create cell from UIPickerView in his delegate method and after that I upload image on server, next store to document path library. I need editing property on my custom cell, attachmentId and here is that problem.

- (void)pushToImageDetailViewController:(id)sender
{
    [MessageLoader invalidate];
    [self.messageInputFieldView resignFirstResponder];

    while (sender && ![sender isKindOfClass:[MessageTableViewCell class]]) {
        sender = [sender superview];
    }
    if (!sender) {
        NSLog(@"Error by getting cell from superview of button");
    } else {
        MessageTableViewCell *cell = (MessageTableViewCell *) sender;
        ImageDetailViewController *imageDetailView = [[ImageDetailViewController alloc] init];
        imageDetailView.sender = cell.sender;
        imageDetailView.attachmentId = cell.attachmentId;
        [[self navigationController] pushViewController:imageDetailView animated:YES];
   }
}

This code I have in block, therefore weakSelf on self property, important is refresh cell statements, indexPathForRow fired cellForRowAtIndexPath, in this message I have NSLog and right ID get into this message.

NSMutableDictionary *messagesFromCollection = [weakSelf.messageDetailViewController.messages objectAtIndex:weakSelf.index];
[[[messagesFromCollection objectForKey:@"attachments"] firstObject] setObject:[fileId copy] forKey:@"id"];
// refresh concret cell
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:weakSelf.index inSection:0];
[weakSelf.messageDetailViewController.messagesTableView reloadRowsAtIndexPaths:@[indexPath] 
                                                                withRowAnimation:UITableViewRowAnimationNone];

in cellForRowAtIndex assigning id from self.messages, Array of Dictionaries collection to cell property attachmentId, but in pushToImageDetail I have old ID before pregenerated.

    cell.attachmentId = [attachmentInfo objectForKey:@"id"];
    [cell.attachmentView addTarget:self action:@selector(pushToImageDetailViewController:)     forControlEvents:UIControlEventTouchUpInside];
    [cell.attachmentView setImage:attachmentImage forState:UIControlStateNormal];
    [cell.bgImageView addSubview:cell.attachmentView];
    [cell.contentView addSubview:cell.attachmentView];

Problem is that id in imageDetailView.attachmentId = cell.attachmentId assigns old id (bad binding image detail with id of image) and in cellForRow is new actual cell.attachmentId = [attachmentInfo objectForKey:@"id"]. I don't know how it is possible.

Here is cellForRow:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
NSDictionary *messagesFromDictionary = [self.messages objectAtIndex:indexPath.row];
NSString *message = [messagesFromDictionary objectForKey:@"message"];
UIImage *attachmentImage = [self.attachments objectAtIndex:indexPath.row];
NSString *time = [NSString formattedTime:[messagesFromDictionary objectForKey:@"posted_date"]];
NSString *beforeSender = [self beforeSender:indexPath];

static NSString *CellIdentifier;
if ([message isEqualToString:@""]) {
    CellIdentifier = @"AttachmentCellIdentifier";
} else {
    CellIdentifier = @"MessageCellIdentifier";
}

MessageTableViewCell *cell = (MessageTableViewCell *)
                            [self.messagesTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
    cell = [[MessageTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

cell.sender = [messagesFromDictionary objectForKey:@"user_id"];
cell.backgroundColor = [UIColor clearColor];

UIImage *bgImage = nil;
UIImage *arrowImage = nil;
CGRect messageFrame = {};
CGRect bgImageFrame = {};
CGRect profilePictureFrame = {};

CGRect timeFrame = {};
CGRect arrowFrame = {};

// Log message level 3
//[NSString LogMessages:[NSString stringWithFormat:@"S:%f, %f M: %@", messageFrame.size.width, messageFrame.size.height, message] :3];

/*
 !!!! width and height for all cells !!!!
 */

cell.timeLabel.text = [NSString stringWithFormat:@"%@", time];
[cell.timeLabel sizeToFit];
timeFrame.size.height += cell.timeLabel.frame.size.height; // MUST BE DEFINED BEFORE Y COORDINATE
timeFrame.size.width += cell.timeLabel.frame.size.width;
profilePictureFrame.size.width += AVATAR;
profilePictureFrame.size.height += AVATAR;

if ([CellIdentifier isEqualToString:@"MessageCellIdentifier"]) {
    cell.messageContentView.text = message;
    CGSize  textSizeFrame = { 320.0 - 5*PADDING - AVATAR - cell.timeLabel.frame.size.width - RESERVE - 12, CGFLOAT_MAX};
    messageFrame.size = [message sizeForText:textSizeFrame withFontSize:MESSAGE_FONTSIZE];
    messageFrame.size.width += RESERVE;
    messageFrame.origin.x += PADDING;
    messageFrame.origin.y += PADDING;

    bgImageFrame.origin.y += PADDING;
    bgImageFrame.size.width = messageFrame.size.width + 3*PADDING  + cell.timeLabel.frame.size.width;
    bgImageFrame.size.height += messageFrame.size.height + 2*PADDING;

    arrowFrame.size.width += MESSAGE_ARROW_WIDTH;
    arrowFrame.size.height += MESSAGE_ARROW_HEIGHT;
    arrowFrame.origin.y += bgImageFrame.size.height/2;

    profilePictureFrame.origin.y += PADDING;

    // must be defined after bgImageFrame height, timeFrame height
    timeFrame.origin.y += bgImageFrame.size.height/2 - cell.timeLabel.frame.size.height/2;
    // must be defined after messageFrame width
    timeFrame.origin.x += messageFrame.size.width + PADDING;
} else if ([CellIdentifier isEqualToString:@"AttachmentCellIdentifier"]) {
    // prepare fields to download attachment from server
    NSArray *attachmentsFromDict = [messagesFromDictionary objectForKey:@"attachments"];
    NSDictionary *attachmentInfo = [attachmentsFromDict objectAtIndex:0];
    NSLog(@"id from cell %@", [attachmentInfo objectForKey:@"id"]);
    cell.attachmentId = [attachmentInfo objectForKey:@"id"];

    bgImageFrame.size.height = attachmentImage.size.height;
    bgImageFrame.size.width = attachmentImage.size.width;

    timeFrame.origin.y += PADDING;
    // must be defined after bgImageFrame width, timeFrame width
    timeFrame.origin.x += bgImageFrame.size.width/2 - timeFrame.size.width/2;

    [cell.attachmentView addTarget:self action:@selector(pushToImageDetailViewController:) forControlEvents:UIControlEventTouchUpInside];
    [cell.attachmentView setImage:attachmentImage forState:UIControlStateNormal];
    cell.attachmentView.tag = indexPath.row;
    [cell.bgImageView addSubview:cell.attachmentView];
    [cell.contentView addSubview:cell.attachmentView];
}

// X, Y positions
if ([cell.sender isEqualToString:[[user userInfo] objectForKey:@"id"]]) { // left aligned (Sender)
    bgImageFrame.origin.x = 320 - bgImageFrame.size.width - PADDING;
    bgImage = [UIImage imageNamed:@"msg_sender.png"];

    arrowFrame.origin.x += 320 - PADDING - 2;
    arrowImage = [UIImage imageNamed:@"sender_arrow.png"];

    cell.messageContentView.backgroundColor = [UIColor  whiteColor];
    cell.timeLabel.backgroundColor = [UIColor  whiteColor];
} else { // right aligned (Receiver)
    bgImageFrame.origin.x += AVATAR + 2*PADDING;
    bgImage = [UIImage imageNamed:@"msg_receiver.png"];

    arrowFrame.origin.x += AVATAR + 2*PADDING - MESSAGE_ARROW_WIDTH + 2;
    arrowImage = [UIImage imageNamed:@"receiver_arrow.png"];

    profilePictureFrame.origin.x += PADDING;

    cell.messageContentView.backgroundColor = [UIColor colorWithRed:255/255.0 green:242/255.0 blue:254/255.0 alpha:1];
    cell.timeLabel.backgroundColor = [UIColor colorWithRed:255/255.0 green:242/255.0 blue:254/255.0 alpha:1];
}

if ([CellIdentifier isEqualToString:@"AttachmentCellIdentifier"]) {
        cell.attachmentView.frame = CGRectMake(IMAGE_WIDTH_PADDING + bgImageFrame.origin.x,
                                               IMAGE_TOP_PADDING + timeFrame.size.height,
                                               bgImageFrame.size.width - 2*IMAGE_WIDTH_PADDING,
                                               bgImageFrame.size.height - IMAGE_BOTTOM_PADING - timeFrame.size.height);
}

if (
    (![cell.sender isEqualToString:beforeSender]) &&
    (![cell.sender isEqualToString:[[user userInfo] objectForKey:@"id"]])
   )
{
    [cell.profilePicture setFrame:profilePictureFrame];

    // ==== load user avatar
    cell.profilePicture.layer.masksToBounds = YES;
    float radius = AVATAR/2;
    CAShapeLayer *circle = [CAShapeLayer layer];
    circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(KAPPA, KAPPA, 2.0*radius, 2.0*radius)
                                             cornerRadius:radius].CGPath;
    [cell.profilePicture.layer setMask:circle];
    cell.profilePicture.image = [user getMyContactAvatar:cell.sender :existingContacts];
    [cell.contentView addSubview:cell.profilePicture];
} else {
    cell.profilePicture.frame = CGRectZero;
    cell.profilePicture.image = nil;
    [cell.profilePicture removeFromSuperview];
}

cell.messageContentView.frame = messageFrame;

[cell.bgImageView setFrame:bgImageFrame];
cell.bgImageView.image = bgImage;

cell.timeLabel.frame = timeFrame;
cell.textLabel.numberOfLines = 0;

cell.arrowView.frame = arrowFrame;
cell.arrowView.image = arrowImage;

cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

Solved by assign into tag property indexPath.row on UIButton, but still don't understand why my previous method didn't work.

- (void)pushToImageDetailViewController:(id)sender
{
[MessageLoader invalidate];
[self.messageInputFieldView resignFirstResponder];

UIButton *imageButton = (UIButton *)sender;
MessageTableViewCell *cell = (MessageTableViewCell *) [self tableView:self.messagesTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:imageButton.tag inSection:0]];        
ImageDetailViewController *imageDetailView = [[ImageDetailViewController alloc] init];
imageDetailView.sender = cell.sender;
imageDetailView.attachmentId = cell.attachmentId;
[[self navigationController] pushViewController:imageDetailView animated:YES];
}

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