ios - 如何在 UITableViewController 的静态单元格中添加 UIView?

标签 ios objective-c uitableview uiview

我有一个静态单元格,用于存储用户可以输入字段的信息表单。其中一个静态单元格用于附件。当用户从 UIImagePickerController 选择图像时,我想将一些 UIView 附加到此单元格中。我已经处理了 UIImagePickerController 部分。我已经通过 reuseIdentifier 找到了单元格:

UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AttachmentCell"];

然后我尝试在我的单元格中附加 View :

CGRect attachmentFrame = CGRectMake(0, 0, 50, 50);

UIView * attachmentRow = [[UIView alloc]initWithFrame: attachmentFrame];

CGFloat ratio = 50 / image.size.width;

CGRect imageFrame = CGRectMake(10, 0, image.size.width * ratio, 50);
CGRect imageNameLabelFrame = CGRectMake(10 + imageFrame.size.width + 10, 22, 300, 6);

UIImageView *imageView = [[UIImageView alloc]initWithFrame: imageFrame];
imageView.image = image;

UILabel *imageNameLabel = [[UILabel alloc]initWithFrame:imageNameLabelFrame];
imageNameLabel.text = [NSString stringWithFormat:@"Attachment %d", attachmentCounter++];

[attachmentRow addSubview: imageView];
[attachmentRow addSubview: imageNameLabel];

attachmentRow.backgroundColor = [UIColor lightGrayColor];

[cell.contentView addSubview: attachmentRow];

NSLog(@"Row appended");

请注意,上面的代码只是尝试在静态单元格中添加一个 View ,但似乎失败了。添加 View 后,我的静态单元格中没有显示任何内容。我已经记录了我的附件数据可以成功地带到这个页面。

最佳答案

也许这只是问题中的措辞,但似乎您在用户选择图像后所做的是创建一个新单元格。

这一行:

UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AttachmentCell"];

创建一个全新的单元格,而你的 TableView 无法知道它。

你应该做的是在用户选择图像并在其中设置后重新加载你的 TableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

为了重新加载数据,您可以使用 reloadData UITableView的方法|这将重新加载所有数据或 - (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *) withRowAnimation:(UITableViewRowAnimation)animation这将仅重新加载指定索引处的单元格。

关于ios - 如何在 UITableViewController 的静态单元格中添加 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36007913/

相关文章:

ios - 添加具有行动画的单元格时,将 tableview 保持在相同的滚动位置

ios - Realm Platform 数据未通过 Storyboard-Swift 4 加载到 TableView 中

ios - 无法下载Xcode 7.3的HomeKit配件模拟器

ios - 如何从实体[核心数据]中获取关系对象

ios - 在不同的 viewController 中使用相同的 UIWebView

objective-c - 快速将 UIColor 转换为 CGColor

iphone - 交换 Root View Controller

ios - Swift 按钮框架高度问题 (viewDidLayoutSubviews)

ios - UITableView 不显示数据并且不添加数据

ios - 在 Xcode 中设置分组单元格的宽度