[英]How can I add UIView in a static cell in UITableViewController?
我有一个静态单元格,用于存储用户可以输入字段的信息表。 静态单元格之一是用于附件的。 当用户从UIImagePickerController选择图像时,我想将一些UIView附加到此单元格中。 我已经处理过UIImagePickerController部分。 我已经通过reuseIdentifier找到了单元格:
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AttachmentCell"];
我试图在单元格中附加视图:
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");
请注意,上面的代码只是尝试在静态单元格中添加一个视图,但似乎失败了。 添加视图后,静态单元格中没有任何显示。 我已经记录了附件数据可以成功带到此页面。
首先添加这一行
[cell.contentView addSubview: attachmentRow];
然后
[attachmentRow addSubview: imageView];
[attachmentRow addSubview: imageNameLabel];
可能会有所帮助
也许这只是问题中的措辞,但似乎在用户选择图像之后您要做的就是创建一个新的单元格。
这行:
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AttachmentCell"];
创建一个全新的单元格,您的表视图无法了解它。
您宁愿做的是在用户选择图像并在其中设置图像后重新加载表格视图
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
为了重新加载数据,您可以使用UITableView
reloadData
方法来重新加载所有数据,也可以使用- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *) withRowAnimation:(UITableViewRowAnimation)animation
来仅重新加载指定索引处的单元格。
您将必须使用自定义单元,而不是使用默认的tableview单元。 为此,首先需要创建一个带有标识符的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.