0

UIImageコンテンツを含むカスタムセルUIButtonを使用し、ターゲットアクションセレクター pushToImageDetailViewController: を使用して、ビューを画像の別の詳細ビューにプッシュします。彼のデリ​​ゲート メソッドで UIPickerView からセルを作成し、その後サーバーに画像をアップロードし、次のストアをドキュメント パス ライブラリに保存します。カスタム セル attachmentId のプロパティを編集する必要がありますが、ここにその問題があります。

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

このコードはブロック内にあるため、自己プロパティのweakSelf、重要なのはリフレッシュセルステートメント、indexPathForRowがcellForRowAtIndexPathを起動したことです。このメッセージにはNSLogがあり、正しいIDがこのメッセージに含まれています。

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

cellForRowAtIndex では、self.messages の Array of Dictionaries コレクションからセル プロパティ attachmentId に ID を割り当てますが、pushToImageDetail では、事前に生成される前に古い ID を持っています。

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

問題は、imageDetailView.attachmentId = cell.attachmentId の id が古い id (画像の id を持つ不適切なバインディング画像の詳細) を割り当て、cellForRow の id が新しい実際の cell.attachmentId = [attachmentInfo objectForKey:@"id"] であることです。それがどのように可能かわかりません。

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

1 に答える 1

1

UIButtonのタグプロパティindexPath.rowに割り当てることで解決しましたが、以前の方法が機能しなかった理由はまだわかりません。

- (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];
}
于 2013-11-13T16:55:44.693 に答える