简体   繁体   中英

How can I add UIView in a static cell in UITableViewController?

I have a static cell that stores an information form which user can input fields. One of the static cell is for attachments. I would like to append some UIView into this cell when the user chose an image from the UIImagePickerController. I have already handled the UIImagePickerController part. And I have find the cell by reuseIdentifier:

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

And I tried to append views in my cell:

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

Notice that the above code is just to try adding one single view in the static cell but it seems failed. Nothing shows in my static cell after I add the view. I have logged that my attachment data can successfully brings to this page.

firstly add this line

[cell.contentView addSubview: attachmentRow];

then

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

may be it will help

Maybe this is just the wording in the question, but it seems that what you do after the user has selected the image, is that you create a new cell.

This line :

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

Creates a brand new cell, and your table view has no way to know about it.

What you should rather do is to reload your table view after the user has selected the image and setup it in

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

In order to reload the data you can use either reloadData method of UITableView which will reload all data or - (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *) withRowAnimation:(UITableViewRowAnimation)animation which will reload only cells at specifed indexes.

You will have to use custom cell instead of using default tableview cell. For that first you need to create one with identifier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM