Custom cell image deforms when scrolling - ios

I have a custom cell for my UITableView ; that custom cell comes from a class that extends UITableViewCell.
There is only a .xib where i crated the actual cell, and the links to these items in the .h.
I'm not using the .m, it's only the autogenerated awakeFromNib and setSelected:selected:animated in there.
When i create my cell in the cellForRow method, it's all fine, it appears correctly on the screen.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"CustomCellTableViewCell";
CustomCellTableViewCell *cell = [self.tbUpcomingEvents dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
tmp = [_eventList objectAtIndex:indexPath.row];
cell.lbDescription.text = tmp.description;
cell.lbTitle.text = tmp.title;
cell.imageView.layer.cornerRadius = 20;
cell.imageView.clipsToBounds = YES;
cell.layer.masksToBounds = YES;
cell.imageView.image = [UIImage imageNamed:tmp.type.typeImage];
cell.lbTimeStart.text = [[[timeFormatter stringFromDate:tmp.startTime] stringByAppendingString:#" - "]stringByAppendingString:[dateFormatter stringFromDate:tmp.startDate]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
My issue is the following : when I start scrolling the tableview, the NEW cells are messed up, specifically the image. It simply gets bigger/wider and (obviously) deformed. I don't really know what caused it or how to fix it.
The cell is associated with the reuse identifier from the cellForRow method and the custom class in the .xib file. The UITableView has nothing in particular (and has no other link than the storyboard -> .h, delegate, datasource)
Any clue?

I found my issue. In this specific case, my imageView in my customCell.h was called "imageView". Which is a reserved named and, i guess, created conflicts at (some?) points in the creation of the cells.
I don't know the why's, but i know what fixed the issue. Now i've simply put another property name for my image and i'm golden ;)

Try to replace your code:
cell.lbDescription.text = tmp.description;
cell.lbTitle.text = tmp.title;
cell.imageView.image = [UIImage imageNamed:tmp.type.typeImage];
cell.lbTimeStart.text = [[[timeFormatter stringFromDate:tmp.startTime] stringByAppendingString:#" - "]stringByAppendingString:[dateFormatter stringFromDate:tmp.startDate]];
from cellForRowAtIndexPath to willDisplayCell method.
The idea is to make UITableViewCells templates in tableView:cellForRowAtIndexPath:, but to fill cells with particular values in tableView:willDisplayCellForRowAtIndexPath:.

uncheck autoresize subviews in interface builder

Related

Swift custom tableViewCell

I have created a custom UITableViewCell with a label on the far left (respects the parent margins) and it displays correctly. However, when I set an image using imageView.image (from the UITableViewCell), the label does not move to the right and so the label and the image are on top of each other. Any ideas how to make the label behave like the default label where it will move to the right to make way for the image?
If you create the cell from storyboard make sure you have set the UITableViewCell style to Custom.
Edit:
I saw your comment above.
If you don't wish to create a custom UITableViewCell class, you can still use default UITableViewCell with style set to Basic
So you can obtain its properties:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = #"Your text";
cell.imageView.image = [UIImage imageNamed:#"Your image name"];
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame: CGRectZero];
cell.accessoryView = mySwitch;
// Define the state of the switch because the cell will get recycled as you scroll.
BOOL isOn;
[mySwitch setOn:isOn];
return cell;
}
The other solution is mentioned as commented above: You have to subclass UITableViewCell, drag and drop 3 IBOutlets and reference them in .h file.

Addview image not displaying in table cell

I modified a subview table cell using addsubview to include an additional image on the right (besides the one on the left). I had it working great until I added a search bar at the top. Somehow in doing that, the right image stopped displaying. I tried removing the search bar but have been unable to get the image to display. I have a feeling I made a typo inadvertently or somehow messed up the code.
Here is the method. It works except that the image on the right no longer displays. Would appreciate it if anyone can spot my error or suggest what is going wrong.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *protoCell = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:protoCell];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:protoCell];
}
// IDModel * item;
IDModel * item = nil;
// if no search
//item = [self.tableItems objectAtIndex:indexPath.row];
//following for search
if (tableView == self.searchDisplayController.searchResultsTableView) {
item = [searchResults objectAtIndex:indexPath.row];
} else {
item = [self.tableItems objectAtIndex:indexPath.row];
}
// Configure the cell...
cell.textLabel.text = item.name;
cell.detailTextLabel.text = item.sub;
cell.imageView.image = [UIImage imageNamed:item.pic];
//following crops and rounds image
//add image to right
UIImageView *rightimage;
rightimage = [[UIImageView alloc] initWithFrame:CGRectMake(225.0,0.0,80.0,45.0)];
rightimage.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
// rightimage.image = [UIImage imageNamed:#"pic.jpg"];
[cell.contentView addSubview:rightimage];
rightimage.image = [UIImage imageNamed:item.pic];
return cell;
}
Thanks in advance for any suggestions.
One concern here is that you shouldn't add views to a UITableViewCell from the view controller. Table view cells are reused, so each time this cell gets reused you'll be adding a new view to it without first removing the old one.
Instead, create a UITableViewCell subclass, and add rightimage (which I would rename rightImageView to be consistent with Apple's naming conventions) in the -initWithStyle:reuseIdentifier: method. Then set the rightView's frame in the -layoutSubviews method, or set up the auto layout constraints in the -initWithStyle:reuseIdentifier: right after creating the rightImageView.
Make rightImageView a public property (put it in the .h file) so you can assign the image from the view controller.

IOS7 Custom TableViewCell will not appear

I am trying to create a project with a custom UITableViewCell. The custom cells never load, they're just blank. At this point in the project what I'm trying to do is placing a UITableViewCell in a .xib, designing it the way I want and specifying its reuse identifier along with tag IDs for the components so that I can use them in code later on.
I've googled a ton and found several tutorials that look like what I want to do, along with many SO questions that have answers that seem applicable. At this point it's probably just my head spinning with too many different angles and solutions.
This is my current attempt at trying to register the custom cell with my UITableView, yet when running this on a device the rows in the table view are entirely blank.
UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyCell"];
}
UILabel* l1 = (UILabel*)[cell viewWithTag:1];
UILabel* l2 = (UILabel*)[cell viewWithTag:2];
UILabel* l3 = (UILabel*)[cell viewWithTag:3];
l1.text = #"Foobar";
l2.text = #"Foobar";
l3.text = #"Foobar";
I'm pretty certain that I've hooked up all the properties and such correctly, but at this stage I need a fresh pair of eyes to point out the facepalm for me.
The interesting files are FilmerView.m/h/xib and the cell is in FilmerViewCell.xib. When running the app this TableView is in the second tab of the tab bar controller.
Project:
http://speedy.sh/WhhpP/test12.zip
I can't provide a full answer atm but look up the tableview method. registerNib:forCellReuseIdentifier:
Also, stop using that dequeue method. Use the one that includes the indexPath.
Then you don't have to check if the cell is nil afterwards.
EDIT
In viewDidLoad (or somewhere like that)...
UINib *cellNib = [UINib nibWithNibName:#"MyCustomCellXibFileName" bundle:[NSBundle mainBundle]];
[self.tableView registerNib:cellNib forCellReuseIdentifier:#"CellIdentifier"];
Now in the table view datasource method...
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier" forIndexPath:indexPath];
// no need to check cell == nil.
// this method is guaranteed to return a non nil cell.
// if it doesn't then the program will crash telling you that you need to...
// register a class or nib (but we just did this in viewDidLoad) :D
// configure your cell here...
[self configureMyCell:(MyCustomCell *)cell atIndexPath:indexPath];
return cell;
}
- (void)configureMyCell:(MyCustomCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
cell.nameLabel.text = #"Hello, world";
}
Hope this helps.
Make sure that you have set datasource and delegate properties of your tableView.
Make sure that you have implemented (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section method and it returns a positive value (>0).
Evaluate the following:
Is the ReuseIdentifier set in the XIB. See Properties Tab in Interface Builder on the right when selecting the cell.
Are the AutoresizingMasks set properly for the labels to be visible?
WrapModes: Which did you select? When having wrapmode WrapWord, is the font size too large for the text to be moved in the next line becoming invisible?
Set the background color of the UITableViewCellss content view to something else than white or transparent, as well as the background colors of the labels to see if the cell is even there.
Manually call numberOfRowsInSection on your table, pass the proper NSIndexPath identifying the target section and see if its greater 0 to confirm that the TableView even attempts to load the data, thus, the cells. ( Alternatively set a breakpoint in the method or do a NSLog. )
Do a NSLog in cellForRowAtIndexPath to confirm that the cell returned is not nil and the method is even called!

Custom uitableviewcell displaying images in alternate cell

Okay, so after checking a lot of SO answers, I am posting this.
I am trying to make a custom a uitableviewCell in which i have one uiimageview and uilabel.
The problem is when i load cells, it loads alternate images in uitableview only, also loads correct data for uilabel for each cell.
It should load images on all the cells And sometimes misplaces the images in wrong cells also
Here is what i am doing...
Step 1:
#define kActivityCell #"ActivityCell"
Step 2:
[self.feedTable registerNib:[UINib nibWithNibName:kActivityCell bundle:nil]
forCellReuseIdentifier:kActivityCell];
Step 3:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// format for feeds
NSLog(#"row no: %d",indexPath.row);
ActivityCell *activityCell = (ActivityCell *)[tableView dequeueReusableCellWithIdentifier:kActivityCell forIndexPath:indexPath];
//selection color
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [GeneralUtils colorWithString:kTableSelectionHEX];
activityCell.selectedBackgroundView = selectionColor;
[activityCell listOfLikeSetting];
ZDUser *newObject = [likeArray objectAtIndex:indexPath.row];
activityCell.nameAndDesc.text = newObject.display_name;
activityCell.nameAndDesc.font = [GeneralUtils FontSettingBold13];
activityCell.objectID = newObject.userID;
activityCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// this method is from AFNetworking Lib
[activityCell.photoLeft setImageWithURL:[NSURL URLWithString:newObject.avatar]];
// this is my own method to cache images, but i am not using it right now.
// [activityCell.photoLeft setProfileImageFor:newObject.avatar];
return activityCell;
}
I have created a separate class for ActivityCell and xib is there in which i have made the connections properly for iboutlet uiimageview photoleft
As per now, i am clueless why its happening anybody pls share your thoughts...
here are some of the links which i have read , but it didn't help me.
Lazy load images in UITableViewCell
UIImageView not showing up in custom UITableViewCell
UITableViewCell load images and reused cells
This problem is solved now, it was because of using the same cell at many places and customising according to needs so there was some internal issue.
I have now created a new class for it and it works fine.
Thanks
Sumit

UIImageView not loading in UITableViewCell until scrolled

I am having a problem with a UIImageView I am setting inside tableview:cellForRowAtIndexPath: for some reason when the UITableView loads the bottom cell half in view dose not have the a Image loaded into the UIImageView like the rest of the UITableViewCells, however once I scroll then it works itself out.. But once doing so the top UItableViewCell then drops its image! untill i scroll that back into full view.
What I have done is created the UITableViewCell in Interface Builder and i have a blank UIImageView, I then set the UIImage I am going to place inside the UIImageView in ViewDidLoad then inside tableview:CellForRowAtIndexPath: I set it then return the cell.
heres the code
//.m
- (void)viewDidLoad
{
//..
storageCalcIconImage = [UIImage imageNamed:#"CALC.png"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
SeriesSearchResultItem *myObj = [[SeriesSearchResultItem alloc] init];
if (indexPath.section == 0) {
//call obj array with all of the values from my sorting algorithum
myObj = (SeriesSearchResultItem*)[dataArrayOfObjects objectAtIndex:indexPath.row];
//.. assinging storageCalcIconImage inside if statment
firstSpaceImage.image = storageCalcIconImage;
//..
}
return cell;
}
there is lots happening in tableView:CellForRowAtIndexPath but I decided to drop the stuff that wasnt related to the image problem.. hopefully you guys can see an error I am making that I havent yet... any help would be greatly appreciated.
This is an expected result from the use of reusable cells inside a tableView. If you want to avoid this behavior you should create a cell for each rows of your tableView instead of using dequeueReusableCellWithIdentifier.
Expect huge performance impact if you have a lot of rows to display though.

Resources