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
Related
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
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!
I made a UICollectionView with some cells in it and it displayed correct, now I want to set a selected tag for one or more cells, in custom cell, I can use two ways to implement it:
way 1: set selectedBackgoundView
self.selectedBackgroundView = backgroundView;
way 2: add a UIImageView as selected tag
[_coverImageView addSubview:_selectImageView];
//coverImageView is image for cell,
//selectImageView is a tag imageView for selected.
then the problem comes up:
For example I selected the first cell, When I scroll the UICollectionView, way 1 still displayed the first cell selected, but with way 2, the _selectImageView would be added to the other cell.
I know it is caused by Reuse Cell,but have no idea for deal with it.
Rather than adding your selected tag after you've created the cell, you should add it at the point of creation.
You don't say how you're creating your custom collection view cells, but it sounds as if you might not be using your own subclass, and are simply adding what you need to a plain UICollectionViewCell. You will find it much easier to create your own subclass, and set it up with an image view exposed that can be enabled/disabled as required. You can create custom cells either entirely in code, or in conjunction with a XIB - whichever you prefer.
Recently I am working on a similar project. Although It's long ago, but I hope to help someone who need it.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionCellID forIndexPath:indexPath];
if (cell == nil) {
cell = [[MyCollectionViewCell alloc]init];
}
//Change Selected State
if([[collectionView indexPathsForSelectedItems] indexOfObject:indexPath] != NSNotFound){
UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 250, 250)];
bgView.backgroundColor = kLightBlueColor;
[cell setSelectedBackgroundView:bgView];
cell.selected = YES;
}
cell.title.text = #"Hello World";
return cell;
}
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.
Can i put an image in the top of an TableViewCell instead of putting it on the left side ? I used this to put it in the cell:
Code:
cell.imageView.image = [UIImage imagenNamed:#"river.png"];
For asynchronous image downloading and showing on table use this:
go to https://github.com/enormego/EGOImageLoading
and download the classes.
then in your .h file:
#class EGOImageView;
EGOImageView *imageIconView; // object for caching the images
and in .m file
#import "EGOImageView.h"
#import "EGOCache.h"
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
imageIconView=[[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:#"icon72x72.png"]];
[self setFlickrPhoto:[NSString stringWithFormat:#"yourimageurl"];
imageIconView.frame=CGRectMake(0, 0, 120, 100);
[cell addSubview:imageIconView];
return cell;
}
- (void)setFlickrPhoto:(NSString*)flickrPhoto
{
imageIconView.imageURL = [NSURL URLWithString:flickrPhoto];
}
You can create a custom cell. You just pick style custom for the cell type. If you are using static cells you can just insert an ImageView in the cell, drag an outlet to the controller header file and set the image in the code.
If you are using dynamic cells its best to create a new class for your cells. Set the custom class for the cell in the interface builder. Create and outlet for the imageview in the custom cell class header. Then when you instantiate cells in the tableView:cellForRowAtIndexPath, set the cell class to the custom cell class and set its property.
There are very good tutorials for storyboards that cover dynamic and static cell tables:
Beginning Storyboards in iOS Part 1
Beginning Storyboards in iOS Part 2
you have set the frame of an imageview in cellforRowAtIndexPath
Yes you can. for this you have to go for custom tableview cell. you can design this using separate XIB for tableview cell. May be this link will help you.
or search on google also for more examples its easy to implement.