UITableViewCell imageView problems - ios

I have a custom leaderboard which I load Game Center profile pictures into a UITableCell that is 32 high.
What happens is when I set the image it displays at around 200x32 then quickly resizes to be a square. The user sees a flash where the image is distorted before it is shown properly.
What is happening here? How can I fix this?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([GCCustomLeaderboard sharedInstance]->nearbyScores.count == 0){
return nil;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyBasicCell"];
GCLeaderboardScore *playerScore = [[GCCustomLeaderboard sharedInstance]->nearbyScores objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%#: (%d)", playerScore->alias, (int)playerScore->score];
cell.imageView.image = playerScore->photo;
return cell;
}

To fix this I had to resize my images BEFORE putting them in the table.

Related

Why the UITableViewCell changed its size when I slide the view

I am a beginner in iOS development. I face a problem where I need little help.
I wrote a small program to learn custom UITableViewCell. It just works well at beginning; after that, when I slide the view, it changes size of the cell. I am confused about where I might be going wrong. The ContentView have only one view which is UIImageView.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
HWHomePageCell *cell = [tableView dequeueReusableCellWithIdentifier:#"HWHomePageCell"];
if (cell == nil)
{
cell = [HWHomePageCell homePageCell];
}
cell.imageView.image = [UIImage imageNamed:#"XD"];
return cell;
}
These are the pictures. Top two are good, but when I slide down, as you can see, the 3rd doesn't work well
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
HWHomePageCell *cell = [tableView dequeueReusableCellWithIdentifier:#"HWHomePageCell"];
cell = [HWHomePageCell homePageCell];
cell.imageView.image = [UIImage imageNamed:#"XD"];
return cell;
}
When you use custom cell then no need to check whether cell is nil or not. so please remove this condition and implement it.
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;

UITableView Cell Data is Repeating When Paging - Objective C

I am using Objective C and Parse. I have a UITableView that displays photos from Parse. The first 10 cells load then you can tap load more for the next 10 cells and so on. My issue is in the same class that I am getting the photo, I also want to display the caption column. The first 10 cells show the caption correctly, it's when I page to the next 10 cells that the captions start repeating. The photos do not repeat so I'm not sure why the captions do. Below is my code that loads the UITableView:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = #"Cell";
if (indexPath.section == self.objects.count) {
// this behavior is normally handled by PFQueryTableViewController, but we are using sections for each object and we must handle this ourselves
UITableViewCell *cell = [self tableView:tableView cellForNextPageAtIndexPath:indexPath];
return cell;
}
else {
ESPhotoCell *cell = (ESPhotoCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ESPhotoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell.mediaItemButton addTarget:self action:#selector(didTapOnPhotoAction:) forControlEvents:UIControlEventTouchUpInside];
}
cell.mediaItemButton.tag = indexPath.section;
//The below line of code displays the black square where the photo would be.
cell.imageView.image = [UIImage imageNamed:#"PlaceholderPhoto"];
if (object) {
//The below line displays the users photo on the timeline.
//The following 2 lines is what fills the display cell that shows the photo.
cell.imageView.clipsToBounds = YES;
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
//The below line is what displays the image on the home timeline.
cell.imageView.file = [object objectForKey:kESPhotoPictureKey];
[cell.imageView loadInBackground];
//Below is what I am trying to display the captions
NSString *caption = [object objectForKey:#"caption"];
if (caption != nil) {
cell.captionText.text = [object objectForKey:#"caption"];
}
}
return cell;
}
}
Any help to this issue would be appreciated as I am fairly new to mobile development.

Reasons for UITableViewCell UI inconsistency

Most of the time, when my app is working the way it should, my Table View items look like this:
But every so often a cell (on initial load) looks likes this:
As you can see the image has resized, the 'published By' label has resized.
Why would this happen? The same code/storyboard should affect all the cells the same way? Why are some not doing what they are told?
If it helps, when a cell loads the wrong way, all I have to do is scroll up, and back down again, and the problem is fixed !!
This means that there clearly isn't a problem with the image or the amount of text, is it just the iPhone acting up?
Thanks for any help !
I think its cell dequeue issue. Your cell could not calculate proper height for cell. If you are using autolayout try the following code. hope it will works for you.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
static YOUR_TABLEVIEW_CELL *sizingCell = nil;
static NSString *CellIdentifier=#"YOUR_TABLEVIEW_CELL_IDENTIFIER";
sizingCell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (sizingCell==nil)
{
sizingCell=[[YOUR_TABLEVIEW_CELL alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[self configureFareIssueCell:sizingCell atIndexPath:indexPath];
return [self calculateHeightForConfiguredSizingCell:sizingCell];
}
//assign all the lables & images here
- (void)configureFareIssueCell:(YOUR_TABLEVIEW_CELL* )cell atIndexPath:(NSIndexPath *)indexPath
{
//e.g
cell.lbl.text=#"YOUR_TEXT";
cell.imageView.image=[UIImage imageNamed:#"NAME_OF_YOUR_IMAGE"];
}
- (CGFloat)calculateHeightForConfiguredSizingCell:(YOUR_TABLEVIEW_CELL *)sizingCell
{
CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height + 1.0f; // Add 1.0f for the cell separator height
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"YOUR_TABLEVIEW_CELL_IDENTIFIER";
YOUR_TABLEVIEW_CELL *cell =[tableView dequeueReusableCellWithIdentifier:#"YOUR_TABLEVIEW_CELL_IDENTIFIER"];
if (cell==nil)
{
cell=[[YOUR_TABLEVIEW_CELL alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[self configureFareIssueCell:cell atIndexPath:indexPath];
return cell;
}
Do you use layer mask for creating rounded image? If yes, you see this strange behavior because layer mask was created before UITableView assign proper frame for cell, so layer mask will have incorrect frame.

UITableView Image in the Cell Shrinks

The images in the tableview cell shrinks as soon as they are scrolled. They resize to the wanted size by reload.
The big stars have the size I want. The smaller ones appears during scrolling or on touch.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"Cell";
NSArray *sectionData = self.dataSource[indexPath.section];
SHFriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
[cell updateWithData:sectionData[indexPath.row]];
return cell;
}
- (void)updateWithData:(SHFriendTableCellData *)data {
self.friendImage.contentMode = UIViewContentModeScaleToFill;
self.friendImage.image = data.image;
self.nameLabel.text = data.name;
}
Changing the content mode has no effect.
I had a double reference to the image view.
After deleting the issue was gone after I deleted the double reference.

UITableViewCell with UIImageView in a few of them

I have a customized UITableViewCell and I've added a UIImageView to it. The thing is, there are times when I want to remove the UIImageView as the data I receive from the server does not have an image associated with it. How do I make the cell layout change such that the other elements move up. Should I set the UIImageView's frame to zero? Using a placeholder image is not a solution.
Here's what my problem looks like:
The row with index 0 does not have an image but there is a space in between the top and the bottom label. Is there a way I can do this using Auto Layout?
Your problem is actually caused by heightForRowAtIndexPath, you have to return the proper value in this method, something like:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
YourCustomObject *object = [tableDataSource objectAtIndex:indexPath.row];
if(object.hasImage) {
return heightForCellWithImageInside;
}
else {
return heightForCellWithoutImageInside;
}
}
Then, in your cellForRowAtIndexPath just hide the UIImageView if there is no image for it.
You can dynamically change the height of cell.If there is an image,then increase cell height otherwise not.Also set the imageView whenever u get image from server otherwise no need to set imageView.like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"ImageCellItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if(imageFromserver)
{
//Set your imageView here
}
else
{
// Do nothing
}
return cell;
}
when ever there is no image coming from server hide or remove your image view and assign the frame of image view to your bottom label.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;
}
if(yourimage==nil)
{
bottomlabel.frame=imageview.frame; // you can do something like this, as per your need.this is just example
}
return cell;

Resources