我有一个静态单元格,用于存储用户可以输入字段的信息表单。其中一个静态单元格用于附件。当用户从 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/