2016-03-15 113 views
0

我有一个静态单元存储用户可以输入字段的信息形式。其中一个静态单元用于附件。我想在用户从UIImagePickerController中选择一个图像时将一些UIView附加到这个单元格中。我已经处理了UIImagePickerController部分。我有找到reuseIdentifier细胞:如何在UITableViewController的静态单元中添加UIView?

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"); 

注意,上面的代码只是为了尝试在静态小区追加一个单一视图但似乎失败了。添加视图后,静态单元格中没有显示任何内容。我记录了我的附件数据可以成功引入此页面。

+0

你的行高是多少? –

+0

我在故事板中将其设置为300 –

+0

您在哪里创建单元格? – JomanJi

回答

1

也许这只是问题的措辞,但似乎你做什么后用户选择了图像,是你创建了一个新的单元格。

这条线:

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

创建一个全新的电池,和你的表视图没有办法知道。

你应该宁可做的是重新载入你的表视图,用户已经选择在

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

图像和设置之后为了重新加载,你可以使用的UITableViewreloadData方法,将刷新数据所有数据或- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *) withRowAnimation:(UITableViewRowAnimation)animation将仅重新加载指定索引处的单元格。

+0

这似乎是重装问题。非常感谢。 –

1

首先加入这一行

[cell.contentView addSubview: attachmentRow]; 

然后

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

可能它会帮助

+0

我刚刚尝试添加附件行,但它不起作用 –

+0

给出了它的视图的边框宽度,它在哪里制作视图.... – 2016-03-15 10:23:13

+0

但我已将背景设置为黑色...... –

0

您将不得不使用自定义单元格而不是使用默认tableview单元格。 首先你需要创建一个带有标识符的文件。

相关问题