【问题标题】:IOS tableview: stretched image not display until scroll doneIOS tableview:拉伸图像直到滚动完成才显示
【发布时间】:2013-09-03 11:04:55
【问题描述】:

我想在我的项目中创建一个聊天气泡视图。我将图像拉伸为气泡。 但是问题发生了,如果我将单元格滚动到不可见,则在单元格回到原始位置后,可以拉伸图像,这是显示拉伸图像的唯一方法。

以下图片是关于问题的简要说明。

故事板中的气泡视图布局,UIImageView 是拉伸图像:

滚动前:

滚动后:

初始显示气泡视图时(其原始显示方法:导航栏项Segue),并在viewWillAppear()中调用拉伸函数,显示正常拉伸。

以下是关键代码: 单元格中的初始气泡拉伸:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
   NSString *cellID = @"chatCell";
    UIBubbleTableViewCell *cell = (UIBubbleTableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellID];
   //CAll stretch function
   cell.dataInternal = dataInternal;
}

拉伸功能:

bubbleImage.image = [[UIImage imageNamed:@"chat2.png"] stretchableImageWithLeftCapWidth:21 topCapHeight:14];
bubbleImage.frame = CGRectMake(40, 10, 267, 105);

每次更新调用拉伸函数:

- (void)viewDidAppear:(BOOL)animated{
[chatTable reloadData];

}

无论如何,只有滚动结束,图像才能被拉伸显示。我调试了项目,并记录了拉伸数据,一切都很好。

我该如何解决这个问题,谢谢您的建议。

【问题讨论】:

    标签: iphone ios objective-c uitableview


    【解决方案1】:

    你要做的是把你的图片拉伸功能放在

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    

    委托方法。只有此时您才会知道要显示的表格单元格的实际大小。然后,您可以将气泡图像拉伸到您想要的内容。

    【讨论】:

    • 我试过你的方法,将拉伸功能移动到 willDisplayCell ,同样的问题发生了。我错过了什么?
    • 发布你的完整代码,让我看看。你错过了一些东西,看到你的拉伸代码只在滚动时被调用。
    • - (void)tableView:(UITableView )tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ UIBubbleTableViewCell *Ccell = (UIBubbleTableViewCell)cell; Ccell.bubbleImage.image = [[UIImage imageNamed:@"chat2.png"] stretchableImageWithLeftCapWidth:25 topCapHeight:14]; Ccell.bubbleImage.frame = CGRectMake(40, 40, 197 + 30, 90 + 15); NSLog(@"Guest bubbleImage.image sectch: %f, %f", cell.bubbleImage.center.x, cell.bubbleImage.center.y );这是我的伸展代码,请检查它。谢谢!!
    • 移动willDisplayCell中的拉伸代码,结果一样,只是将tableview滚动到顶部(单元格更新),图片可以正常拉伸。
    猜你喜欢
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多