UICollectionView with FlowLayout not scrolling when setting section insets - objective-c

I am using a UICollectionView programmatically.
I've set its frame as follows:
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 3000) collectionViewLayout:flowLayout];
My UICollectionViewFlowLayout has a section inset set as:
flowLayout.sectionInset = UIEdgeInsetsMake(20.0, 20.0, 100.0, 20.0);
I also only have 1 section.
I've set the height to be 3,000 to see if the collection view would scroll but it doesn't. I am setting it to that since the collection view doesn't seem to scroll up or down when I insert new items.
UICollectionView is subclassed from a UIScrollView, which means I can keep resetting the scroll view content size. Would this work properly?
The only issue is that this would work if I know the sizes of all items including how many items are on each row.
If each item had a different size, how would I find out the size of each row? I would need that to add all row sizes and increase the content size height of self.collectionView.
EDIT
Even my suggestion above, resetting the content size, does not work properly! I tried the following when updating my collection view:
int numberOfRows = self.dataArray.count / 3;
self.collectionView.contentSize = CGSizeMake(self.view.frame.size.width, (numberOfRows * 200) + 300);
This is very ugly though. This is because it assumes all my images are of size 200x200px, which fits 3 images per row (hence the division by 3).
Furthermore, even with the +300 that I have, the last row I can only see about 3/4 of it and not all of it. I have to scroll more and I will be able to see it but it goes back up again to 3/4. Its kind of like the scroll to refresh, where the view only moves if you drag it and when you leave it bumps back to where it was. Why is this happening? Is it just that Apple designed UICollectionView so poorly? This is a little ridiculous... UITableViews adjust their scroll according to their content automatically.

In answering some of your other questions about UICollectionView, I created this demo project, and it scrolls just fine, so I don't know what problem you're having. The only thing I needed to do to make the last row wholly visible was to increase the size of the bottom inset to 90. I also added a completion method to the performBatchUpdates:completion: to automatically scroll so that the last added item is visible (notice that I add some extra items by using performSelector;withObject:afterDelay:). My images are 48x48, and I just set my collection view to the bounds of my controller's view. Here is the code so you can compare it to what you have:
#interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> {
NSMutableArray *newData;
}
#property (nonatomic, retain) UICollectionView *collectionView;
#property (nonatomic, retain) NSMutableArray *results;
#end
#implementation ViewController
- (void)viewDidLoad {
self.results = [NSMutableArray array];
int i = 11;
while (i<91) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"New_PICT00%d.jpg",i]];
[self.results addObject:image];
i++;
}
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
self.view.backgroundColor = [UIColor blackColor];
[self.view addSubview:self.collectionView];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"Cell"];
//[self.collectionView registerClass:[RDCell class] forCellWithReuseIdentifier:#"myCell"];
[self.collectionView reloadData];
[self performSelector:#selector(addNewImages:) withObject:nil afterDelay:3];
}
-(void)addNewImages:(id)sender {
newData = [NSMutableArray array];
int i = 102;
while (i<111) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"New_PICT0%d.jpg",i]];
[newData addObject:image];
i++;
}
[self addNewCells];
}
-(void)addNewCells {
NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];
[self.collectionView performBatchUpdates:^{
int resultsSize = [self.results count];
[self.results addObjectsFromArray:newData];
for (int i = resultsSize; i < resultsSize + newData.count; i++)
[arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
[self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths];
}
completion: ^(BOOL finished){
[self.collectionView scrollToItemAtIndexPath:arrayWithIndexPaths.lastObject atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES];
}];
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return [self.results count];
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
//cell.iv.image = [self.results objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor colorWithPatternImage:[self.results objectAtIndex:indexPath.row]];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
UIImage *image = [self.results objectAtIndex:indexPath.row];
return CGSizeMake(image.size.width, image.size.height);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(10, 10, 90, 10);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Selected Image is Item %d",indexPath.row);
}

Setting the height of the UICollectionView to 3000 will make your scrolling problem worse. If the UICollectionView is 3000 pixels tall then it will only ever need to scroll if it has more than 3000 pixels worth of content in it. You should set it to the height of the view it is contained in (same as the width).
AFAIK the also should not set the contentSize directly, instead you need to override the - (CGSize)collectionViewContentSize method in a subclass of UICollectionViewLayout, but generally speaking you shouldn't need to change the content size for normal uses.
I'm not totally clear on what your problem is - is it that when you add more than a screens worth of items you can't scroll?

I've encountered the same problem because I was initializing the elements of my collectionView (i.e. it's DataSource) in the viewWillAppear method.
I finally add just a [self.collectionView reloadData]; and it works just fine!
Maybe you're in the same case.

Related

UICollectionView cell imageview not show image, but it's show background color xcode [duplicate]

I have an issue with the images not showing, even though they are loading. I have tried this several different ways and the same result... no images. I do get white rectangles that are the size and color specified in the Storyboard. I get the correct number of rectangles in the popover. The log shows the names and ids correctly.
If I call the array directly, I get the same result... white rectangles.
My target is iOS7. Running Xcode 5.0.2. Images are coming from a SQL database. This is a live app, which I am updating to full iOS7 and where I am swapping out a custom grid layout for UICollectionView. Using a CollectionViewController, embedded into a NavigationController, which is accessed via a UIButton. As you can see, there is no custom cell. The images are to show in a popover and are then selectable by the user to change the background of the underlying view.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"BckgndCell" forIndexPath:indexPath];
NSDictionary * tempDict = [_arrayOfBckgnds objectAtIndex:indexPath.row];
NSNumber *buttonTagNumber = (NSNumber *)[tempDict objectForKey:#"id"];
int buttonTag = [buttonTagNumber intValue];
NSString *tempImage = [tempDict objectForKey:#"fileName"];
NSLog(#"Filename: %#", tempImage);
NSLog(#"ButtonTagNumber: %#", buttonTagNumber);
UIImageView *image = [[UIImageView alloc]init];
image.image = [UIImage imageNamed:tempImage];
NSLog(#"Image.image: %#", image.image);
// needed for selecting the background in didSelectItemAtIndexPath
_bgtag = buttonTag;
return cell;
}
The fix which includes actually naming the BackgroundCell (duh) in the cellForItemAtIndexPath method and creating a small method in the BackgroundCell controller to set the image.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
BackgroundCell *cell = (BackgroundCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"BckgndCell" forIndexPath:indexPath];
NSDictionary * tempDict = [_arrayOfBckgnds objectAtIndex:indexPath.row];
NSNumber *buttonTagNumber = (NSNumber *)[tempDict objectForKey:#"id"];
int buttonTag = [buttonTagNumber intValue];
NSString *tempImage = [tempDict objectForKey:#"fileName"];
[cell setImage:[UIImage imageNamed:tempImage]];
// needed for selecting the background in didSelectItemAtIndexPath
_bgtag = buttonTag;
return cell;
}
The main cell code;
- (id)initWithFrame:(CGRect)frame
{
_backgroundImage = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
[self.contentView addSubview:_backgroundImage];
if (self) {
// Initialization code
}
return self;
}
-(void)setImage:(UIImage *)image
{
_backgroundImage.image = image;
}
The problem is that the image is not being set in the cell's view hierarchy. To do that, subclass UICollectionViewCell and create a imageView property in the subclass:
#interface CollectionViewCellSubclass : UICollectionViewCell
#property (nonatomic, strong) UIImageView *imageView;
#end
...and init the UIImageView in its initWithFrame:
_imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
[self.contentView addSubview:_imageView];
Set this subclass as the Custom Class for the cell in storyboard and then load it in the collectionView:cellForItemAtIndexPath method above setting the image to the cell's image subview:
CollectionViewCellSubclass *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"BckgndCell" forIndexPath:indexPath];
//...
cell.imageView.image = [UIImage imageNamed:tempImage];
Hope this helps.
Adding cell.contentView.frame = [cell bounds] after the line dequeueReusableCellWithReuseIdentifier worked for me.
Suppose you add a custom (200,200) UICollectionViewCell through xib and change its frame somewhere in the code to be e.g. (120,120). You have to set its content view’s frame accordingly, otherwise image on its imageView (which is on its content view) will not be shown properly.

UITableView swap cells

In my UITable view, i want to be able to switch the positions of two cells when you drag one on to the other. I have already completed this task. The only problem is that when you try to drag one over the other, all the rows that follow shift down to make room for the new one. I do not want this, I want the cells to essentially stay visually static when you are dragging the first cell around. Any ideas?
Thanks,
Mitch
I think you can do this with some trickery, which is after all, how Apple does a lot of its animations.
Start with a UIViewController with a table view as a subview
Add a pan gesture recognizer to the main view (you'll have to implement shouldRecognizeSimultaneouslyWithGestureRecognizer: and return YES, so it will work with the table view's own gesture recognizers)
When you start to drag, create a snapshot view of the cell you started the drag over, and add it as a subview of the main view. You'll probably want to disable the table view's scrolling at this point also.
Drag that snapshot view using the pan gesture recognizer's translationInView property
When you drop the view, delete the snapshot view, and update the table's data source to show the data in the new order you created with the move.
I haven't tried all of this yet (but I've used some elements of it in other projects), but I think this should give you a start on what you're trying to achieve. There are some more details to work out for sure -- what do you want to see in the spot from where you dragged the cell? A blank space? What do you want to see when the dragged view is dropped?
After Edit:
This is what I have so far, and it works pretty well. In the storyboard, I have a UITableViewController with two cell prototypes, both basic types. The one whose identifier is "Blank" just has no text in its label. The tableviewController is embedded in a navigation controller, and I've added a button to the navigation bar with the initial title of "Drag" -- this button is connected to the toggleDragging method.
#interface TableController ()
#property (strong,nonatomic) NSMutableArray *theData;
#property (strong,nonatomic) UIPanGestureRecognizer *panner;
#property (strong,nonatomic) UIView *cellSnapshotView;
#property (strong,nonatomic) NSIndexPath *draggingCellIndexPath;
#end
#implementation TableController
- (void)viewDidLoad {
[super viewDidLoad];
self.panner = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(moveCellImage:)];
[self.tableView addGestureRecognizer:self.panner];
self.panner.enabled = NO;
self.panner.delegate = self;
self.draggingCellIndexPath = [NSIndexPath indexPathForRow:-1 inSection:0];
self.theData = [#[#"One",#"Two",#"Three",#"Four",#"Five",#"Six",#"Seven",#"Eight",#"Nine",#"Black",#"Brown",#"Red",#"Orange",#"Yellow",#"Green",#"Blue",#"Violet",#"Gray",#"White"] mutableCopy];
}
-(IBAction)toggleDragging:(UIBarButtonItem *)sender {
if ([sender.title isEqualToString:#"Drag"]) {
self.panner.enabled = YES;
sender.title = #"Scroll";
}else{
self.panner.enabled = NO;
sender.title = #"Drag";
self.tableView.scrollEnabled = YES;
}
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
-(IBAction)moveCellImage:(UIPanGestureRecognizer *)panner {
if (! self.cellSnapshotView) {
CGPoint loc = [panner locationInView:self.tableView];
self.draggingCellIndexPath = [self.tableView indexPathForRowAtPoint:loc];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:self.draggingCellIndexPath];
self.cellSnapshotView = [cell snapshotViewAfterScreenUpdates:YES];
self.cellSnapshotView.alpha = 0.8;
self.cellSnapshotView.layer.borderColor = [UIColor redColor].CGColor;
self.cellSnapshotView.layer.borderWidth = 1;
self.cellSnapshotView.frame = cell.frame;
[self.tableView addSubview:self.cellSnapshotView];
self.tableView.scrollEnabled = NO;
[self.tableView reloadRowsAtIndexPaths:#[self.draggingCellIndexPath] withRowAnimation:UITableViewRowAnimationNone]; // replace the cell with a blank one until the drag is over
}
CGPoint translation = [panner translationInView:self.view];
CGPoint cvCenter = self.cellSnapshotView.center;
cvCenter.x += translation.x;
cvCenter.y += translation.y;
self.cellSnapshotView.center = cvCenter;
[panner setTranslation:CGPointZero inView:self.view];
if (panner.state == UIGestureRecognizerStateEnded) {
UITableViewCell *droppedOnCell;
CGRect largestRect = CGRectZero;
for (UITableViewCell *cell in self.tableView.visibleCells) {
CGRect intersection = CGRectIntersection(cell.frame, self.cellSnapshotView.frame);
if (intersection.size.width * intersection.size.height >= largestRect.size.width * largestRect.size.height) {
largestRect = intersection;
droppedOnCell = cell;
}
}
NSIndexPath *droppedOnCellIndexPath = [self.tableView indexPathForCell:droppedOnCell];
[UIView animateWithDuration:.2 animations:^{
self.cellSnapshotView.center = droppedOnCell.center;
} completion:^(BOOL finished) {
[self.cellSnapshotView removeFromSuperview];
self.cellSnapshotView = nil;
NSIndexPath *savedDraggingCellIndexPath = self.draggingCellIndexPath;
if (![self.draggingCellIndexPath isEqual:droppedOnCellIndexPath]) {
self.draggingCellIndexPath = [NSIndexPath indexPathForRow:-1 inSection:0];
[self.theData exchangeObjectAtIndex:savedDraggingCellIndexPath.row withObjectAtIndex:droppedOnCellIndexPath.row];
[self.tableView reloadRowsAtIndexPaths:#[savedDraggingCellIndexPath, droppedOnCellIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}else{
self.draggingCellIndexPath = [NSIndexPath indexPathForRow:-1 inSection:0];
[self.tableView reloadRowsAtIndexPaths:#[savedDraggingCellIndexPath] withRowAnimation:UITableViewRowAnimationNone];
}
}];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.theData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.draggingCellIndexPath isEqual:indexPath]) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Blank" forIndexPath:indexPath];
return cell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = self.theData[indexPath.row];
return cell;
}
This may be helpful, you can respond however you like to the delegate calls to achieve this effect:
https://github.com/alfiehanssen/AMHEditableTableView
Let me know if you have any questions,

UICollectionView images not showing

I have an issue with the images not showing, even though they are loading. I have tried this several different ways and the same result... no images. I do get white rectangles that are the size and color specified in the Storyboard. I get the correct number of rectangles in the popover. The log shows the names and ids correctly.
If I call the array directly, I get the same result... white rectangles.
My target is iOS7. Running Xcode 5.0.2. Images are coming from a SQL database. This is a live app, which I am updating to full iOS7 and where I am swapping out a custom grid layout for UICollectionView. Using a CollectionViewController, embedded into a NavigationController, which is accessed via a UIButton. As you can see, there is no custom cell. The images are to show in a popover and are then selectable by the user to change the background of the underlying view.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"BckgndCell" forIndexPath:indexPath];
NSDictionary * tempDict = [_arrayOfBckgnds objectAtIndex:indexPath.row];
NSNumber *buttonTagNumber = (NSNumber *)[tempDict objectForKey:#"id"];
int buttonTag = [buttonTagNumber intValue];
NSString *tempImage = [tempDict objectForKey:#"fileName"];
NSLog(#"Filename: %#", tempImage);
NSLog(#"ButtonTagNumber: %#", buttonTagNumber);
UIImageView *image = [[UIImageView alloc]init];
image.image = [UIImage imageNamed:tempImage];
NSLog(#"Image.image: %#", image.image);
// needed for selecting the background in didSelectItemAtIndexPath
_bgtag = buttonTag;
return cell;
}
The fix which includes actually naming the BackgroundCell (duh) in the cellForItemAtIndexPath method and creating a small method in the BackgroundCell controller to set the image.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
BackgroundCell *cell = (BackgroundCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"BckgndCell" forIndexPath:indexPath];
NSDictionary * tempDict = [_arrayOfBckgnds objectAtIndex:indexPath.row];
NSNumber *buttonTagNumber = (NSNumber *)[tempDict objectForKey:#"id"];
int buttonTag = [buttonTagNumber intValue];
NSString *tempImage = [tempDict objectForKey:#"fileName"];
[cell setImage:[UIImage imageNamed:tempImage]];
// needed for selecting the background in didSelectItemAtIndexPath
_bgtag = buttonTag;
return cell;
}
The main cell code;
- (id)initWithFrame:(CGRect)frame
{
_backgroundImage = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
[self.contentView addSubview:_backgroundImage];
if (self) {
// Initialization code
}
return self;
}
-(void)setImage:(UIImage *)image
{
_backgroundImage.image = image;
}
The problem is that the image is not being set in the cell's view hierarchy. To do that, subclass UICollectionViewCell and create a imageView property in the subclass:
#interface CollectionViewCellSubclass : UICollectionViewCell
#property (nonatomic, strong) UIImageView *imageView;
#end
...and init the UIImageView in its initWithFrame:
_imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
[self.contentView addSubview:_imageView];
Set this subclass as the Custom Class for the cell in storyboard and then load it in the collectionView:cellForItemAtIndexPath method above setting the image to the cell's image subview:
CollectionViewCellSubclass *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"BckgndCell" forIndexPath:indexPath];
//...
cell.imageView.image = [UIImage imageNamed:tempImage];
Hope this helps.
Adding cell.contentView.frame = [cell bounds] after the line dequeueReusableCellWithReuseIdentifier worked for me.
Suppose you add a custom (200,200) UICollectionViewCell through xib and change its frame somewhere in the code to be e.g. (120,120). You have to set its content view’s frame accordingly, otherwise image on its imageView (which is on its content view) will not be shown properly.

UITableView content of cell dont move on editing

I have a UITableView with some custom cells. In each cell, there is a ImageView and three labels and get the data from a string array. I have done the layout in my storyboard. The data source is a string array. This works.
Now I have insert a EditButton in the code. Now i can see the EditButton, but when I activate the edit mode the table cell will be resized, but the images and labels dont move.
Can you show me how to move the content of the cell? Who knows a tutorial with UITableView uses EditMode AND storyboards. All tutorials which I have found are based on the "old" Xcode.
Thank you very much
By the way, here is my code:
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
myData = [NSMutableArray arrayWithObjects:
#"Line1_Label1|Line1_Label2|Line1_Label3",
#"Line2_Label1|Line2_Label2|Line2_Label3",
#"Line3_Label1|Line3_Label2|Line3_Label3",
nil];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [myData count];
}
// Return a cell for the table
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// A cell identifier which matches our identifier in IB
static NSString *CellIdentifier = #"CellIdentifier";
// Create or reuse a cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Get the cell label using its tag and set it
NSString *currentItem = [myData objectAtIndex:indexPath.row];
NSArray *itemArray = [currentItem componentsSeparatedByString:#"|"];
UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
[cellLabel setText:itemArray[0]];
UILabel *cellLabel2 = (UILabel *)[cell viewWithTag:3];
[cellLabel2 setText:itemArray[1]];
UILabel *cellLabel3 = (UILabel *)[cell viewWithTag:4];
[cellLabel3 setText:itemArray[2]];
// get the cell imageview using its tag and set it
UIImageView *cellImage = (UIImageView *)[cell viewWithTag:2];
[cellImage setImage:[UIImage imageNamed: #"control.png"]];
return cell;
}
// Do some customisation of our new view when a table item has been selected
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure we're referring to the correct segue
if ([[segue identifier] isEqualToString:#"ShowSelectedMovie"]) {
// Get reference to the destination view controller
ItemViewController *vc = [segue destinationViewController];
// get the selected index
NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
// Pass the name and index of our film
[vc setSelectedItem:[NSString stringWithFormat:#"%#", [myData objectAtIndex:selectedIndex]]];
[vc setSelectedIndex:selectedIndex];
}
}
#end
First of all, make an IBOutlet of the tableview in the .h and synthesize it in the .m.
Then make an action to the edit button (if you don't already have one). In the action, write:
CGRect rect = yourTableView.cell.contentView.frame;
//Do whatever changes you wish to do with the sizing of the view. origin changes placement and size changes size (duh). Line below is an example.
rect.origin.y = yourTableView.cell.contentView.frame.origin.y - 20;
yourTableView.cell.contentView.frame = rect;
This won't be animated, but I think it'll fulfill your purpose.
Overwrite the -(void)layoutSubviews{} - method of your custom UITableViewCellController.m or if you don't use a custom UITableViewCellController, try it in your UITableViewController. But I haven't tried it yet with no custom UITableViewCellController.
Something like this will do the trick:
-(void) layoutSubviews {
[super layoutSubviews];
CGFloat xPositionOfElementInTableCell = 273.0f; /* the position of the element before going into edit mode */
if (self.isEditing && !self.showingDeleteConfirmation) // if we enter editing mode but not tapped on the red minus at the moment
{
xPositionOfElementInTableCell = 241.0f;
} else if (self.isEditing && self.showingDeleteConfirmation) // after we tappet on the red minus
xPositionOfElement = 193.0f;
}
CGRect frameOfElementInTableCell = self.myElementInTableCell.frame;
frameOfElementInTableCell.origin.x = xPositionofElement;
self.myElementInTableCell.frame = frameOfElementInTableCell;
}
I hope it helps you. The idea for this code is not mine. I found it here in SO, too. Don't know where exactly.

Sizing a Custom UITableViewCell and included UIImageView based on Image Size

I am trying to customize the UITableViewCell below for an iPhone app in a grouped table view. What I would like to do is have the image width take up the whole cell minus padding (280) and the height variable based on the image size.
Currently I am using SDWebImage to asynchronously download remote images. This may not be the correct thing to do in this case. I am also having trouble figuring out how to give the custom cell the image on initialization. The image URL is stored in self.beerPhoto in the DetailViewController.
I have searched for this a number of ways and have not found exactly what I am looking for. The closest was this: How to scale a UIImageView proportionally, but this method seems to require the cell to have the image at initialization, as I tried to make this code work but setting the image after initialization left a blank cell.
The current code includes constants I set to approximate an image in portrait orientation. In reality some of the images are portrait and some are landscape orientation.
Please let me know if there's anything additional you need to know.
Header for custom UITableViewCell:
#import <UIKit/UIKit.h>
#interface BeerDetailHead : UITableViewCell {
UILabel *beerName;
UIImageView *beerImage;
}
#property(nonatomic, retain)UILabel *beerName;
#property(nonatomic, retain)UIImageView *beerImage;
#end
Relevant portion of implementation for custom UITableViewCell
#import "BeerDetailHead.h"
#implementation BeerDetailHead
#synthesize beerName, beerImage;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
//beerName = [[UILabel alloc]init];
//beerName.textAlignment = UITextAlignmentLeft;
//beerName.font = [UIFont systemFontOfSize:14];
beerImage = [[UIImageView alloc]init];
//[self.contentView addSubview:beerName];
[self.contentView addSubview:beerImage];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+10 ,10, 280, 375);
beerImage.frame = frame;
}
DetailViewController's cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:
[self.sortedKeys objectAtIndex:[indexPath section]]];
NSLog(#"listData = %#", listData);
NSUInteger row = [indexPath row];
if ([self.sortedKeys objectAtIndex:[indexPath section]] == #"header"){
static NSString *headerTableIdentifier = #"HeaderTableIdentifier";
BeerDetailHead * headerCell = (BeerDetailHead*)[tableView
dequeueReusableCellWithIdentifier: headerTableIdentifier];
if(headerCell == nil) {
headerCell = [[[BeerDetailHead alloc] initWithFrame:CGRectZero reuseIdentifier:headerTableIdentifier] autorelease];
}
headerCell.beerName.text = [listData objectAtIndex:row];
[headerCell.beerImage setImageWithURL:[NSURL URLWithString:self.beerPhoto]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
//NSLog(#"frame = %#", headerCell.beerImage.frame);
return headerCell;
}
else{
//use standard UITableViewCell
}
}
Implement tableView:heightForRowAtIndexPath: delegate method and return calculated height for each row from this method.
After loading each image call reloadData on your tableView OR if you want to animate changes call:
[self.tableView beginUpdates];
[self.tableView endUpdates];
Also, you might want to combine several sequence height updates into one. I would use I little delay to perform this:
// triggerUpdates calls the above code
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(triggerUpdates) object:nil];
[self performSelector:#selector(triggerUpdates) withObject:nil afterDelay:0.1];