I have UITableViewCell class, which contains customLabel and imageView. When I try to load them in my main UITableViewController, nothing happens.
Main UITableViewController contains:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell.customButton setTitle:#"Test" forState:UIControlStateNormal];
PFUser *user = [self.members objectAtIndex:indexPath.row];
cell.customLabel.text = [user objectForKey:#"Name"];
PFFile *userImage = [user objectForKey:#"Image"];
[userImage getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
cell.imageView.image = [UIImage imageWithData:data];
[cell setNeedsLayout];
}
}];
return cell;
}
UserTableViewCell contains:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.customLabel = [[UILabel alloc] initWithFrame:CGRectMake(3, 5, 165, 30)];
self.customLabel.font = [UIFont systemFontOfSize:14];
self.customLabel.textColor = [UIColor blackColor];
self.customLabel.backgroundColor = [UIColor clearColor];
self.customLabel.highlightedTextColor = [UIColor whiteColor];
self.customLabel.adjustsFontSizeToFitWidth = YES;
[self.contentView addSubview:self.customLabel];
self.customButton = [[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:CGRectMake(180, 5, 40, 30)];
[self.customButton addTarget:self action:#selector(logButtonRow:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.customButton];
self.imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(60, 1, 50, 50)];
[self.contentView addSubview:self.imageView2];
}
return self;
}
Have I missed something? I've set Prototype cell identifier to "Cell" and custom class to UserTableViewCell(is this required?)
It appears that you set up your cell in IB, and, if so, initWithStyle:reuseIdentifier: will not be called. If you want to add UI elements to your cell in code, then you should implement initWithCoder instead. Alternatively, you could register your class (in viewDidLoad of your table view controller), and that will cause initWithStyle:reuseIdentifier: to be called (the cell in IB would then be superfluous, since the table view will get the cell from your class definition).
[self.tableView registerClass:[UserTableViewCell class] forCellReuseIdentifier:#"Cell"];
You need to alloc and init your cell when its called the first time:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil)
{
// initialize your cell here
cell = ...
}
...
Related
I have tableview in one of my controller. It is not loading data into custom labels and image view with tags. I checked each and every thing, delegates are attached and I am reloading the data in viewWillAppear. I do not understand, where is the problem. I added label and images and assign them tag. Only default label of tableview is showed.
NSString *cellIdentifier;
NSMutableArray *historyArray;
#implementation CloudHistory
-(void)viewDidLoad
{
[super viewDidLoad];
historyArray = [[NSMutableArray alloc]initWithObjects:#"history1",#"history2",#"history3",#"history4",nil];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
dispatch_async(dispatch_get_main_queue(), ^{
//Reload data in services table.
[_historyTableView reloadData];
});
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//Number of section in services table.
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [historyArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
cellIdentifier = #"HistoryCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
UILabel *name = (UILabel*)[cell viewWithTag:40];
name.text =[historyArray objectAtIndex:indexPath.row];
UIImageView *image = (UIImageView*)[cell viewWithTag:44];
image.image = [UIImage imageNamed:#"rtb_logo"];
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//Change the background color to stoker Cloud color.
cell.selectedBackgroundView = [UIView new];
cell.selectedBackgroundView.backgroundColor = [ UIColor colorWithRed:116.0/255.0 green:174.0/255.0 blue:220.0/255.0 alpha:1.0];
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(BOOL)prefersStatusBarHidden
{
return YES;
}
#end
maybe you can export the name and image , I think some of them maybe is invalid or nil .
UILabel *name = (UILabel*)[cell viewWithTag:40];
NSLog(#"name = %#",name); // is it valid ?
UIImageView *image = (UIImageView*)[cell viewWithTag:44];
NSLog(#"image = %#", image); // is it valid ?
if some of them invalid or nil,you can do something like this:
UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 80, 44)];
name.text = #"name";
[cell addSubview:name];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 5, 44, 44)];
imageView.image = image;
[cell addSubview:imageView];
hope it helps.
Did you create a custom UITableViewCell using a xib file?
If so, you could register it in viewdidload.
Something like this
[self.tableView registerNib:[UINib nibWithNibName:#"xibName" bundle:nil] forCellReuseIdentifier:CellIdentifier];
Then your tableview can find your custom view outlets.
Hope this helps
Why don't you use a custom UITableViewCell with your UILabel and UIImageView? So you can access the cell property, like:
cell.name.text = [historyArray objectAtIndex:indexPath.row];
cell.image = [UIImage imageNamed:#"rtb_logo"];
Anyway looks like your UILabel and UIImage is getting the values, but it's not setting them to your cell. To test you can log the value of the UILabel just to check. I don't know if it will work but have you tried this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
cellIdentifier = #"HistoryCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
UILabel *name = (UILabel*)[cell viewWithTag:40];
name.text =[historyArray objectAtIndex:indexPath.row];
[cell viewWithTag:40] = name; // I don't know if this works
// Check it out if the name.text is getting any value
NSLog(#"UILabel %#",name.text);
UIImageView *image = (UIImageView*)[cell viewWithTag:44];
image.image = [UIImage imageNamed:#"rtb_logo"];
[cell viewWithTag:44] = image; // I don't know if this works
return cell;
}
Hi i am beginner in Ios and in my project i have added some labels and image on table-list cell ok they have added so for everything is ok
Here my main requirement is when we tapped on table-list cell labels colors must be change and image need to be change as like below image
for this i have written some code but that's not working pleas help me
my code:-
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cells=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cells];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cells];
}
strip = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 7, cell.frame.size.height)];
strip.backgroundColor = [UIColor orangeColor];
[cell.contentView addSubview:strip];
TitleLbl = [[UILabel alloc] initWithFrame:CGRectMake(65, 7, 130, 35)];
TitleLbl.text =[Right_Menu_array objectAtIndex:indexPath.row];
TitleLbl.textColor=[UIColor darkGrayColor];
TitleLbl.font = [UIFont systemFontOfSize:15];
[cell.contentView addSubview:TitleLbl];
img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 35, 35)];
img.image = [UIImage imageNamed:[imageArr1 objectAtIndex:indexPath.row]];
[cell.contentView addSubview:img];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
TitleLbl.textColor = [UIColor redColor];
strip.backgroundColor = [UIColor redColr];
img.image = [UIImage imageNamed:#"rahul.png"];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
TitleLbl.textColor = [UIColor darkGrayColor];
strip.backgroundColor = [UIColor whiteColor];
img.image = [UIImage imageNamed:#"rahul.png"];
}
The easy way would be to create a custom UITableViewCell and then in the delegate functions of UITableView get the instance of CustomTableViewCell and set the Labels textColor.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{
CustomTableViewCell *cell =(CustomTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.TitleLbl.textColor = [UIColor redColor];
cell.strip.backgroundColor = [UIColor redColr];
cell.img.image = [UIImage imageNamed:#"rahul.png"];
}
PS: there are many other work arounds for this...it depends on how do you want to implement the code.
Other way,
//set flag variable as
#property (strong ,nonatomic) NSIndexPath *selectedIndexPath;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.selectedIndexPath = indexPath;
[tableView reloadData];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cells=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cells];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cells];
}
strip = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 7, cell.frame.size.height)];
strip.backgroundColor = [UIColor orangeColor];
TitleLbl = [[UILabel alloc] initWithFrame:CGRectMake(65, 7, 130, 35)];
TitleLbl.text =[Right_Menu_array objectAtIndex:indexPath.row];
TitleLbl.textColor=[UIColor darkGrayColor];
TitleLbl.font = [UIFont systemFontOfSize:15];
img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 35, 35)];
img.image = [UIImage imageNamed:[imageArr1 objectAtIndex:indexPath.row]];
if(indexPath == selectedIndexPath)
{
TitleLbl.textColor = [UIColor redColor];
strip.backgroundColor = [UIColor redColr];
img.image = [UIImage imageNamed:#"rahul.png"];
}
[cell.contentView addSubview:strip];
[cell.contentView addSubview:TitleLbl];
[cell.contentView addSubview:img];
}
//This code is not tested.
I suppose you are referring to the tableview header.
You can only set tableview header view to your own custom view using the tableHeaderView property of the tableview. You can set the background color of your custom header view normally.
E.g.
self.tableView.tableHeaderView = myHeaderView;
Alternatively, if you want to modify the section header view, you can use one of the following delegate methods:
tableView:viewForHeaderInSection:
dequeueReusableHeaderFooterViewWithIdentifier:
and return your custom header view there.
From the code, I can see you hold the reference for properties of last known strip, TitleLbl and img, which will always be the components from last UITableViewCell accessed from
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Since your cell has an orange strip on left side, I would prefer creating a custom UITableViewCell to hold the references.
#interface CustomCell : UITableViewCell
#property (nonatomic,strong) UIView *strip;
#property (nonatomic,strong) UILabel *titleLbl;
#property (nonatomic,strong) UIImageView *imgView;
#end
then with a custom cell, your code can be
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = #"cellIdentifier";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// create and add components
cell.strip = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 7, cell.frame.size.height)];
[cell.contentView addSubview:cell.strip];
cell.titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(65, 7, 130, 35)];
[cell.contentView addSubview:cell.titleLbl];
cell.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 35, 35)];
[cell.contentView addSubview:cell.imgView];
}
// modify existing components
cell.strip.backgroundColor = [UIColor orangeColor];
cell.titleLbl.text = [Right_Menu_array objectAtIndex:indexPath.row];
cell.titleLbl.textColor = [UIColor darkGrayColor];
cell.titleLbl.font = [UIFont systemFontOfSize:15];
cell.imgView.image = [UIImage imageNamed:[imageArr1 objectAtIndex:indexPath.row]];
if ([cell isSelected]) {
// remember to keep the changes for cell in it's selected state
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell = (CustomCell *) [tableView cellForRowAtIndexPath:indexPath];
cell.titleLbl.textColor = [UIColor redColor];
cell.strip.backgroundColor = [UIColor redColor];
cell.imgView.image = [UIImage imageNamed:#"rahul.png"];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *) [tableView cellForRowAtIndexPath:indexPath];
cell.titleLbl.textColor = [UIColor darkGrayColor];
cell.strip.backgroundColor = [UIColor whiteColor];
cell.imgView.image = [UIImage imageNamed:#"rahul.png"];
}
also you can override the method in your custom cell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
I have a UITableView that's loading some custom UITableViewCells. These custom cells alloc a few UIButtons, and add those buttons to the custom cell's contentView. When repeatedly calling reloadData on the tableView, it seems these UIButtons are slowly leaking memory. I am using ARC, iOS 7.
Here's where the cell is created:
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"PostCell";
PostCell *cell = [_tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[PostCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
return cell;
}
And here's the init method for the custom cell:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.height = 0;
self.backgroundColor = [UIColor clearColor];
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self addInteractionContainer];
}
return self;
}
And finally the addInteractionContainer method (if I don't call this method, I don't see a leak):
- (void)addInteractionContainer {
//interaction container
UIView* interactionContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 80)];
[interactionContainer setBackgroundColor:[UIColor ProseSepiaColor]];
//like button
LikeButton* likeButton = [[LikeButton alloc] initWithFrame:CGRectMake(10, 10, 75, 30) liked:([post.yourVote intValue] > 0)];
[likeButton addTarget:self action:#selector(likePost:) forControlEvents:UIControlEventTouchUpInside];
[interactionContainer addSubview:likeButton];
//comment button
CommentButton* commentButton = [[CommentButton alloc] initWithFrame:CGRectMake(likeButton.frame.origin.x + likeButton.frame.size.width + 10,
10, 105, 30)];
[commentButton addTarget:self action:#selector(segueToComments:) forControlEvents:UIControlEventTouchUpInside];
[commentButton setNumComments:self.post.numComments];
[interactionContainer addSubview:commentButton];
[self.contentView addSubview:interactionContainer];
self.height += interactionContainer.frame.size.height;
}
try this
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"PostCell";
PostCell *cell = [_tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if(cell!=nil)
{
cell=nil;
}
if (cell == nil) {
cell = [[PostCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
return cell;
}
I have table view in side bar which is similar to facebook app. In my sidebar tableview i have imageview in each row. I want the imageview will be changed when i clicked the cell and it will be retain while for selecting another cell. Here is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
[[cell.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];
UIImageView * cellImg = [[UIImageView alloc]initWithFrame:CGRectMake(5,8,259, 60)];
[cellImg setImage:[UIImage imageNamed:[menuArr objectAtIndex:indexPath.row]]];
[contentView addSubview:cellImg];
[contentView setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:contentView];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
delegate.sideBarSelectedIndex = indexPath.row;
NSLog(#"delegate.sideBarSelectedIndex: %d",delegate.sideBarSelectedIndex);
[UIView commitAnimations];
if (self.sidebarDelegate) {
NSObject *object = [NSString stringWithFormat:#"ViewController%d", indexPath.row];
[self.sidebarDelegate sidebarViewController:self didSelectObject:object atIndexPath:indexPath];
[delegate.firstLensePageObj timerFunction:indexPath.row];
}
}
Try this in your didSelectRowAtIndexPath delegate method :-
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:#"yourImage"];
Try this in cellForRowAtIndexPath.
UIImageView *selectedImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Your Image"]];
cell.selectedBackgroundView = selectedImageView;
Need to capture the selected indexpath in didSelectRowAtIndexPath: method and need to reload the tableview in that method. In cellForRowAtIndexPath need to check the selected index with current index.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
[[cell.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];
UIImageView * cellImg = [[UIImageView alloc]initWithFrame:CGRectMake(5,8,259, 60)];
if (delegate.sideBarSelectedIndex == indexpath.row){
//Selected cell
[cellImg setImage:[UIImage imageNamed:[selectedMenuArr objectAtIndex:indexPath.row]]];
}else {
//Unselected cell
[cellImg setImage:[UIImage imageNamed:[menuArr objectAtIndex:indexPath.row]]];
}
[contentView addSubview:cellImg];
[contentView setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:contentView];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
delegate.sideBarSelectedIndex = indexPath.row;
NSLog(#"delegate.sideBarSelectedIndex: %d",delegate.sideBarSelectedIndex);
[UIView commitAnimations];
if (self.sidebarDelegate) {
NSObject *object = [NSString stringWithFormat:#"ViewController%d", indexPath.row];
[self.sidebarDelegate sidebarViewController:self didSelectObject:object atIndexPath:indexPath];
[delegate.firstLensePageObj timerFunction:indexPath.row];
}
}
I am displaying 100 remote images in tableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//static NSString *CellIdentifier = #"Cell";
NSString *CellIdentifier = [NSString stringWithFormat:#"%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.imageView.image = nil;
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// Configure the cell...
for (UIView *view in cell.contentView.subviews) {
if ([view isKindOfClass:[UIButton class]] || [view isKindOfClass:[UILabel class]]||[view isKindOfClass:[UIImageView class]]) {
[view removeFromSuperview];
}
}
int imageNumber = 0;
if (isInSearchMode)
{
PhotoVO *photoVO = (PhotoVO *)[searchResultArray objectAtIndex:indexPath.row];
UIImageView *photo_View = [[UIImageView alloc]initWithFrame:CGRectMake(20, 5, width , height - 10)];
photo_View.tag = 101;
[[photo_View layer] setBorderWidth:3.0f];
[[photo_View layer] setBorderColor:[UIColor whiteColor].CGColor];
[photo_View setImageWithURL:[NSURL URLWithString:photoVO.thumb_URL1] placeholderImage:[UIImage imageNamed:#"loader"]];
[cell.contentView addSubview:photo_View];
UILabel *stringLable=[[UILabel alloc]initWithFrame:CGRectMake(130, 20, 150, 30)];
stringLable.backgroundColor=[UIColor clearColor];
stringLable.text=photoVO.photoName;
stringLable.font=[UIFont systemFontOfSize:16.0];
[cell.contentView addSubview:stringLable];
UILabel *tagLable=[[UILabel alloc]initWithFrame:CGRectMake(130, 55, 150, 30)];
tagLable.backgroundColor=[UIColor clearColor];
tagLable.text=photoVO.tagString;
tagLable.font=[UIFont systemFontOfSize:12.0];
[cell.contentView addSubview:tagLable];
}
else
{
for (int i = (indexPath.row * imagesCount); i < ((indexPath.row *imagesCount) + imagesCount); i++) {
if (i < [cellImageVOArray count]) { // If resultsArray Count is odd then we no need to create cell image
PhotoVO *photoVo = (PhotoVO *)[cellImageVOArray objectAtIndex:i];
UIButton *appIconBtn = [UIButton buttonWithType:UIButtonTypeCustom];
appIconBtn.frame = CGRectMake(((imageNumber * 5)+5)+(imageNumber * width), 2, width, height -4);
appIconBtn.tag = i + 100;
[[appIconBtn layer] setBorderWidth:3.0f];
[[appIconBtn layer] setBorderColor:[UIColor whiteColor].CGColor];
[appIconBtn addTarget:self action:#selector(imageTapped:) forControlEvents:UIControlEventTouchUpInside];
[appIconBtn setBackgroundImageWithURL:[NSURL URLWithString:photoVo.thumb_URL1] placeholderImage:[UIImage imageNamed:#"loader.png"]];
//[appIconBtn setBackgroundImageWithURL:[NSURL URLWithString:photoVo.thumb_URL1]];
[cell.contentView addSubview:appIconBtn];
imageNumber ++;
}
}
}
return cell;
}
I am using the above code for displaying the images in tableView, but I get a memory warning in all ways I check it. I think the cell is created every time so please tell me if you see any problem in the code.
This is a problem: NSString *CellIdentifier = [NSString stringWithFormat:#"%d",indexPath.row];
You aren't reusing anything because you're creating a new identifier for each cell. Its fine to have a couple different cell styles reusable, but you're just creating a new cell for every single row.
Second, You need to think about what you're doing here:
for (UIView *view in cell.contentView.subviews) {
if ([view isKindOfClass:[UIButton class]] || [view isKindOfClass:[UILabel class]]||[view isKindOfClass:[UIImageView class]]) {
[view removeFromSuperview];
}
}
Everytime a cell is needed you're removing the parts that make the cell, then remaking them right after. You should be reusing as much as possible in a UITableView. You should look into creating a custom subclass of UITableViewCell that has the pieces you need, then use that. That being said it looks like you just have an image and two labels which a default UITableViewCell would have so you can probably not have to create them at all unless your cell is extremely custom.
Finally, you should look at what you're doing with isInSearchMode. Right now you basically have an if statement for the entire table. Thats not a horrible thing but if you do that you should have two cell identifiers, one for each possible cell. Then in the if statement just swap cell identifiers and fill in the appropriate data.
Above all, if at all possible (which it seems to be in your case) you should not be creating new views in this method at all. You should let the UITableViewCell handle that.
Creating Custom Cells
You start with a simple subclass of UITableViewCell. Then you can add a property for each custom part you need like a UILabel or UIImageView. And you can either create those by overriding init, or you could put them in a custom property getter that creates them on demand.
// CustomCell.h
#import <UIKit/UIKit.h>
#interface Custom : UITableViewCell
#property (strong, nonatomic) UILabel *titleLabel;
#end
// CustomCell.m
#import "CustomCell.h"
#implementation CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(12.0, 10.0, self.contentView.frame.size.width - 24.0, 22.0)];
[self.titleLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[self.titleLabel setHighlightedTextColor:[UIColor whiteColor]];
[self.titleLabel setFont:[UIFont boldSystemFontOfSize:17.0]];
[self.titleLabel setBackgroundColor:[UIColor clearColor]];
[self.titleLabel setTextColor:[UIColor blackColor]];
[self.titleLabel setAdjustsFontSizeToFitWidth:YES];
[self.titleLabel setMinimumFontSize:8.0];
[self.contentView addSubview:self.titleLabel];
}
return self;
}
#end
Then you just need to rewrite your cellForRowAtIndexPath: to use your custom class. And in your case you could have two custom cells and switch between them. This will create only enough of each cell on demand and reuse them as they move on and off screen.
static NSString *CellIdentifier = #"Cell";
static NSString *SearchCellIdentifier = #"SearchCell";
if (isInSearchMode) {
SearchCell *cell = (SearchCell *)[tableView dequeueReusableCellWithIdentifier:SearchCellIdentifier];
if (cell == nil) {
cell = [[SearchCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.titleLabel = #"Custom Search Title";
} else {
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.titleLabel = #"Custom Title";
}
This could easily be refactored even further depending on how your application works but this should get you on the right path.
Yes, you have different cell identifiers for every row, so there is no reusing happening.
Change:
NSString *CellIdentifier = [NSString stringWithFormat:#"%d",indexPath.row];
to
NSString *CellIdentifier = #"CellId";