in my cell data is loading but its is all messed up until i scroll down then if i scroll back everything format correctly. so when every i load listing which is uitableview data is messed up and as soon i start scrolling and scroll back up it gets in place and show correctly. please help.
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:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
for(UIView *v in [cell subviews])
{
if([v isKindOfClass:[UILabel class]])
[v removeFromSuperview];
if ([v isKindOfClass:[UIImageView class]])
[v removeFromSuperview];
}
UIView *CellBGView = [[UIView alloc] initWithFrame:CGRectMake(10, 5, cell.frame.size.width-20, cell.frame.size.height-10)];
CellBGView.backgroundColor = [UIColor whiteColor];
UIImageView *divider = [[UIImageView alloc] initWithFrame:CGRectMake(CellBGView.frame.size.width-155, CellBGView.frame.size.height-140, 150, 150)];
divider.image = [UIImage imageNamed:#"icon_alpha.png"];
[CellBGView addSubview:divider];
......
return cell;
}
Here is out put when i open listing and before scrolling table
here is after scrolling everything gets in place
I have a solution for you.
First, create subclass of UITableViewCell (For example, name it DemoTableViewCell)
In initWithStyle method of DemoTableViewCell, add your images and labels to cell.
In cellForRowAtIndexPath, you don't need add or remove images and labels. Just change color or set image for them.
Related
I am using UITableview cell for listing a set of questions and 2 textfield is for score 1 and score 2 i inputed the score and total showed on a label after that when am scrolling time the inputed data is disappear on the UITableview .Some one please help me to solve this issue and am adding these textfield and label on the view and this view is the subview of cell.contentview
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
else{
for (UIView* view in cell.contentView.subviews) {
if ([view isKindOfClass:[UIView class]]) {
[view removeFromSuperview];
}
}
}
text field added as
UIView * vwbackground = [[UIView alloc] init];
[vwbackground setFrame: CGRectMake(5.0, 5.0, [UIScreen mainScreen].bounds.size.width-10, 90)];
vwbackground.backgroundColor = [UIColor whiteColor];
[cell.contentView addSubview:vwbackground];
self.txtfldInPut1.clearButtonMode = UITextFieldViewModeWhileEditing;
self.txtfldInPut1.autocorrectionType = UITextAutocorrectionTypeNo;
self.txtfldInPut1.inputAccessoryView = numberToolbar;
[vwbackground addSubview:self.txtfldInPut1];
This is because data in table cell is keep on changing as per list items when scrolls . This is a feature of cells in table view .
what you need to do is at the time when put enter your score just update that value to the array/dictonary(whatever you have taken) from which you are intially showing your data in the list.
Hope it helps :)
I know this question is already asked many times, but my problem is some different.
I am creating a UIView and a UIImageView programmatically in cell's content view. When TableView appear first time it looking perfect, but when i scroll down and up , this seems overlapped.
Screenshot of without scroll:
Screenshot after scroll:
Code that i follow:
viewForHead = [[UIView alloc]initWithFrame:CGRectMake(cell.viewForContents.frame.origin.x, cell.viewForContents.frame.origin.y-10, cell.viewForContents.frame.size.width, 45)];
viewForHead.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:255.0/255.0 blue:16.0/255.0 alpha:1];
[cell.contentView addSubview:viewForHead];
UIImageView *imageViewForDP = [[UIImageView alloc]initWithFrame:CGRectMake(viewForHead.frame.origin.x-50, viewForHead.frame.origin.y-8, 60,60 )];
imageViewForDP.image = [UIImage imageNamed:#"dog_1.png"];
//[cell.viewForContents addSubview:imageViewForDP];
imageViewForDP.layer.cornerRadius = 30;
imageViewForDP.clipsToBounds = YES;
[viewForHead addSubview:imageViewForDP];
Please get me out from this problem . Thanks
Use this into your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
if ([cell.contentView subviews]){
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
}
You are adding your viewForHead as a subview each time the cell gets dequeued. So you're adding them on top of each other.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CELL"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"CELL"] autorelease];
// This is where you CREATE your cell contents.
viewForHead = [[UIView alloc]initWithFrame:CGRectMake(cell.viewForContents.frame.origin.x, cell.viewForContents.frame.origin.y-10, cell.viewForContents.frame.size.width, 45)];
viewForHead.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:255.0/255.0 blue:16.0/255.0 alpha:1];
[cell.contentView addSubview:viewForHead];
UIImageView *imageViewForDP = [[UIImageView alloc]initWithFrame:CGRectMake(viewForHead.frame.origin.x-50, viewForHead.frame.origin.y-8, 60,60 )];
imageViewForDP.image = [UIImage imageNamed:#"dog_1.png"];
// [cell.viewForContents addSubview:imageViewForDP];
imageViewForDP.layer.cornerRadius = 30;
imageViewForDP.clipsToBounds = YES;
imageView.tag = 1
[viewForHead addSubview:imageViewForDP];
}
// this is where you UPDATE your viewForHead image and any other elements inside your cell
UIImageView *imageView = [cell.contentView viewWithTag:1];
imageView.image = // your new image
return cell;
}
Subclassing your UITableViewCell and building your layout with a xib would be even better, then you could just access the cells properties directly. A much cleaner solution.
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier#"CELL"];
if (cell == nil) {
cell = [[MyCustomCell alloc] init]; // you ID is set in interface builder
}
cell.imageView.image = // your new image here.
cell.someLabel.text = #"some new text here"
This problem is because of table view cell gets reuse and you are adding a view again on cell.
Try below code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MyIdentifier"] ;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
UIView *viewForHead = (UIView *)[cell.contentView viewWithTag:1];
if (viewForHead==nil) {
viewForHead = [[UIView alloc]initWithFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, 20)];
viewForHead.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:255.0/255.0 blue:16.0/255.0 alpha:0.5];
viewForHead.tag = 1;
[cell.contentView addSubview:viewForHead];
}
return cell;}
I have a Static UITableView with 4 custom Cells. As the cell's are static so it refers that the whole thing is in UITableViewController. I put some random image inside the cells. Now I want to keep my first cell fixed at the top of my tableView. And the rest three cell will be scrolled. I can't do it by putting an extra UIImageView top of the UITableView, because it is static cell. Then, How can I do that?
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
if ((indexPath.row)==0)
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"one"];
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 170)];
backgroundView.image=[UIImage imageNamed:#"default-cover-image.jpg"];
[cell.contentView addSubview:backgroundView];
}
if ((indexPath.row)==1)
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"two"];
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
backgroundView.image=[UIImage imageNamed:#"sectionHeader.png"];
[cell.contentView addSubview:backgroundView];
}
if ((indexPath.row)==2)
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"three"];
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
backgroundView.image=[UIImage imageNamed:#"sectionHeader2.png"];
[cell.contentView addSubview:backgroundView];
}
if ((indexPath.row)==3)
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"four"];
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 170)];
backgroundView.image=[UIImage imageNamed:#"default-cover-image.jpg"];
[cell.contentView addSubview:backgroundView];
}
return cell;
}
Cells are made to be scrolled with the others, if you have a plain style table view you can simply use a header for the section.
If you have one section all the cells will be scrolled under the header view.
To make an easy experiment just add this code
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
And make it returns a string. If you need more customization use this delegate method:
- (UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section
I am used to set another UIViewController containing one UIImageView and one UITableViewController. Just like following:
You can choose View Container in Object Library, just like that:
After running, it's like this:
To accomplish what you described you got 2 options.
If you are using storyboards it's very simple. Just add a view above the tableView with the height of the cell you want and use autolayout to keep them together, like so:
you can see the lower yellow view, it acts like the header of the tableView but it will not scroll down once the user scrolls the table View down
if you are not using storyboard you will have to create the view by code, or create a header for your tableView. The only problem with headers is that they will scroll downwards if the user scroll down
I don't know is it possible or not. But I find another solution to accomplish the task. Here, The static cells are scrolling as usual but I put some extra space top of the tableView and put an UIImage in that empty position.
Set the frame of my Static tableView with CGRectMake(0, 170, 320, 960):
[self.tableView setFrame:CGRectMake(0, 170, 320, 960)];
It put some black space in that empty space. So I put the background as white.
[[[UIApplication sharedApplication] keyWindow] setBackgroundColor:[UIColor whiteColor]];
Then add an UIImageView in that empty space (Here you can't add UIImage in tableView, you have to add that image in superView).
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 170)];
backgroundView.image=[UIImage imageNamed:#"default-cover-image.jpg"];
[self.view.superview addSubview:backgroundView];
So the code will looks like this:
- (void)viewDidAppear:(BOOL)animated
{
// Set the frame of the UITableViewController table
[self.tableView setFrame:CGRectMake(0, 170, 320, 960)];
[[[UIApplication sharedApplication] keyWindow] setBackgroundColor:[UIColor whiteColor]];
// Set the image in superView (not in tableView) as subView on top of the tabelView
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 170)];
backgroundView.image=[UIImage imageNamed:#"default-cover-image.jpg"];
[self.view.superview addSubview:backgroundView];
}
Try to use a normal cell and use it as a header while the other you index them + 1 for displaying
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayPeople count]<1 ? 0 : arrayPeople.count-1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(arrayPeople.count>0)
{
RankingCell *cell =nil;
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RankingCell" owner:nil options:nil];
for (id currentObject in topLevelObjects)
if ([currentObject isKindOfClass:[RankingCell class]])
{
cell = (RankingCell *)currentObject;
break;
}
NSDictionary *element=[arrayPeople objectAtIndex:0];
element=[element dictionaryByReplacingNullsWithStrings];
[cell setupCell:[element objectForKey:#"username"] title2:[element objectForKey:#"total"] title3:[element objectForKey:#"place"] remoteImage:[element objectForKey:#"image"] forPath:[NSIndexPath indexPathForItem:0 inSection:0]];
return cell;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RankingCell";
RankingCell *cell =(RankingCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RankingCell" owner:nil options:nil];
for (id currentObject in topLevelObjects)
if ([currentObject isKindOfClass:[RankingCell class]])
{
cell = (RankingCell *)currentObject;
break;
}
}
NSDictionary *element=[arrayPeople objectAtIndex:indexPath.row+1];
element=[element dictionaryByReplacingNullsWithStrings];
[cell setupCell:[element objectForKey:#"username"] title2:[element objectForKey:#"total"] title3:[element objectForKey:#"place"] remoteImage:[element objectForKey:#"image"] forPath:[NSIndexPath indexPathForItem:indexPath.row+1 inSection:0]];
return cell;
}
I'm trying to to something like apple's alarm clock, when tap the edit button, a custom view cover the custom UITableViewCell.
The code above:
// CGRect frame = [tableView rectForRowAtIndexPath:indexPath];
// CGPoint yOffset = self.tableViewBlock.contentOffset;
// CGRect newFrame = CGRectMake(frame.origin.x, (frame.origin.y - yOffset.y + 45), frame.size.width, frame.size.height);
CallBlock_Date_EditMode *viewController = [[CallBlock_Date_EditMode alloc] initWithNibName:#"CallBlock_Date_EditMode" bundle:nil];
// self.view.frame = newFrame;
// [self.view addSubview:viewController.view];
// [self addChildViewController:viewController];
UITableViewCell *cell = (UITableViewCell*)[self.tableViewBlock cellForRowAtIndexPath:indexPath];
[cell.contentView addSubview:viewController.view];
Cover the specific cell when I put in under:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Just to make sure the size is ok (although when I tap a button in that xib the app crash without even a single error).
But I want to do like apple's alarm clock (actually, mimic it), tap my edit button and my custom UITableViewCell will get cover with this xib as a view.
Maybe there is a better approach to do it?
EDIT:
My updated code is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CallBlock_TableCell *cell = (CallBlock_TableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[CallBlock_TableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(CallBlock_TableCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
cell.accessoryType = self.isEditing ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
CallBlock_ByDate *callBlock = (CallBlock_ByDate*)[fetchedObjects objectAtIndex:indexPath.row];
cell.labelTime.text = callBlock.startDate;
cell.labelRepeat.text = callBlock.repeat;
cell.labelTextLabel.text = callBlock.label;
cell.switchCallBlock.on = YES;
cell.switchCallBlock.tag = (NSInteger)indexPath.row +1;
[cell.switchCallBlock addTarget:self action:#selector(handleSwitch:) forControlEvents:UIControlEventValueChanged];
cell.switchCallBlock.hidden = self.isEditing ? YES : NO;
if (self.isEditing)
{
cell.switchCallBlock.hidden = YES;
UIButton *btnArrow = [[UIButton alloc] init];
btnArrow.frame = CGRectMake(282.0, 31.0, 18.0, 21.0);
[btnArrow setBackgroundImage:[UIImage imageNamed:#"arrow_FWR_off"] forState:UIControlStateNormal];
[btnArrow setBackgroundImage:[UIImage imageNamed:#"arrow_FWR_on"] forState:UIControlStateHighlighted];
btnArrow = [UIButton buttonWithType:UIButtonTypeCustom];
[btnArrow addTarget:self action:#selector(handleTapToEdit:) forControlEvents:UIControlEventTouchUpInside];
btnArrow.tag = indexPath.row + 1;
[cell.contentView addSubview:btnArrow];
[cell.contentView bringSubviewToFront:btnArrow];
}
}
But I cannot get the btnArrow appear on the UTableView.
The reason you are getting a crash is because nothing is retaining your CallBlock_Date_EditMode view controller. You add its view to your cell as a subview, but nothing maintains a reference to the view controller, so it is deallocated and then, when pressing a button that is supposed to pass a message to your view controller, it is sent to a deallocated object and you get a crash.
There are two possible solutions to this. First, you could store that view controller in one of your properties to maintain a reference to it so that it does not deallocated. This is, for the most part, probably not what you want.
Instead, what I would suggest doing is do not make your CallBlock_Date_EditMode a UIViewController, but instead make it a UIView. You may be wondering "But how can I use a xib without a UIViewController?". I would do something like the following:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UIView *view = [[[NSBundle mainBundle] loadNibNamed:#"CallBlock_Date_EditMode" owner:self options:nil] objectAtIndex:0];
self.myEditButton = (UIButton *)[view viewWithTag:2];
[self addSubview:view];
}
return self;
}
This would be code inside your custom UIView that would load in a xib file and add it as a subview. In order to get access to your subviews, you have to use tags inside interface builder, so you do lose the convenience of drawing/connecting IBOutlets... But in the end, it is much better than allocating/storing a bunch of unnecessary UIViewControllers.
If I understand you right and you want to mimic the functionality of the alarm clock that comes pre-installed from Apple, your solution is much simpler than creating a custom view. It looks like all they do is set the On-Off switches to hidden and add a disclosure indicator to the cell. This is what I would do...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
bool hide = (tableView.editingStyle == UITableViewCellEditingStyleDelete); // set to true or false depending on if the table is in editing mode
for (UIView *sv in [cell subviews] ) {
if([sv isKindOfClass:[UISwitch class]]) { // find the on-off switch
[sv setHidden:hide]; // hide the switch depending on the t/f value of hide
break;
}
}
if(hide) { // adds the arrow like in apple's alarm clock table if the cell is in edit mode
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
return cell;
}
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";