I used the following code to implement cell for every row at index path:
But the problem is when I scroll the tableView, the cell will load a lot of UIImageView *itemimageview in one cell in one line, I tried use
for (UIImageView *sView in cell.subviews) {
[sView removeFromSuperview];
}
but it would remove all subviews of one cell. How to solve this problem?...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
NSUInteger oldRow = [lastIndexPath row];
static NSString *CheckMarkCellIdentifier = #"CheckMarkCellIdentifier";
//dequeueReusableCellWithIdentifier --
// Returns a reusable table-view cell object located by its identifier.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CheckMarkCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CheckMarkCellIdentifier] autorelease];
}
/*
for (UIImageView *sView in cell.subviews) {
[sView removeFromSuperview];
}
*/
UIImageView *itemimageview=[[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 232, 54)];
itemimageview.image=[UIImage imageNamed:[tabsImageArray objectAtIndex:row]];
itemimageview.userInteractionEnabled = YES;
[cell.contentView addSubview:itemimageview];
[itemimageview release];
UIImageView *dictIcon=[[UIImageView alloc]initWithFrame:CGRectMake(30, 18, 30, 30)];
dictIcon.image=[UIImage imageNamed:#"dictionary_icon.png"];
dictIcon.userInteractionEnabled = YES;
[cell.contentView addSubview:dictIcon];
[dictIcon release];
UILabel *dictNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 23, 100, 21)];
dictNameLabel.text = dictName;
dictNameLabel.textColor = [UIColor whiteColor];
dictNameLabel.shadowColor = [UIColor blackColor];
dictNameLabel.backgroundColor = [UIColor clearColor];
dictNameLabel.userInteractionEnabled = YES;
[cell.contentView addSubview:dictNameLabel];
[dictNameLabel release];
//cell.textLabel.text = [tabsImageArray objectAtIndex:row];
cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
Consider running through and checking [view isKindOfClass:[UIImageView class]] to check whether the view is the right type of view to remove.
Also consider tagging the views with the UIView tag property, so you can add the subviews once, and then won't have to recreate them with reuse.
Here is how you would do this:
#define ImageViewOneTag 1001
#define ImageViewTwoTag 1002
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)path {
static NSString *CellID = #"CellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
UIImageView *imageViewOne = nil;
UIImageView *imageViewTwo = nil;
if (cell) {
// You've caught a reusable cell. Fetch the image views by their tag.
imageViewOne = (UIImageView *)[cell viewWithTag:ImageViewOneTag];
imageViewTwo = (UIImageView *)[cell viewWithTag:ImageViewTwoTag];
} else {
// You haven't got a reusable cell. Make one, and make and add the image views to the contentView.
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
imageViewOne = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 20.0, 20.0)];
[imageViewOne setTag:ImageViewOneTag];
imageViewTwo = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 20.0, 20.0, 20.0)];
[imageViewTwo setTag:ImageViewTwoTag];
UIView *contentView = cell.contentView;
[contentView addSubview:imageViewOne];
[contentView addSubview:imageViewTwo];
}
// By this stage, you've either retrieved a reusable cell, or you've made a new one. Either way, imageViewOne and imageViewTwo now have a reference to the views you mean.
imageViewOne.image = *imageOneForRow*;
imageViewTwo.image = *imageTwoForRow*;
return cell;
}
I suggest you using custom table view cell, just create a cocoa touch class which inherits uitableviewcell.
Related
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;}
This is how my table is populated :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CellNewsInfo *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
// Set up the cell
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString *titleArticle=[[stories objectAtIndex: storyIndex] objectForKey: #"title"];
titleArticle = [titleArticle stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (indexPath.row==0) {
scr=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
scr.tag = 1;
scr.autoresizingMask=UIViewAutoresizingNone;
[cell addSubview:scr];
[self setupScrollView:scr];
UIPageControl *pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(120, 170, 80, 36)];
[pgCtr setTag:12];
pgCtr.backgroundColor = [UIColor clearColor];
pgCtr.numberOfPages=5;
pgCtr.tintColor=[UIColor redColor];
pgCtr.pageIndicatorTintColor=[UIColor blueColor];
self.pageControl.hidden=YES;
pgCtr.currentPageIndicatorTintColor = [UIColor redColor];
pgCtr.autoresizingMask=UIViewAutoresizingNone;
[cell addSubview:pgCtr];
}
else{
cell.title.text=titleArticle;
cell.title.numberOfLines=2;
why when i scroll it , the first cell is reloading ? i just want to have that scroll view only once at the beginig .
The reason your scrollview is being added again is that the cells are being reused once they are deallocated.
You should look into creating your own custom cells if you are going to display multiple cells types in one tableView, or even using two different cell identifiers depending on if the row is 0.
CellNewsInfo *cell;
if (indexPath.row == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:#"scrollCell" forIndexPath:indexPath];
if ([cell viewWithTag:1]) {
scr = [cell viewWithTag:1];
}
else {
scr=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
scr.tag = 1;
}
// continue customization here with scrollview
}
else {
cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
// continue customization here without scroll view
}
return cell;
I seem to be having a problem understanding how subviews work. I have a UITextfield and I want to add this to the content view of a UITableViewCell so i do this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
} // Configure the cell...
UITextField *tf = [textFieldsArray objectAtIndex:[indexPath row]];
tf.frame = CGRectMake(0, 0, 100, 12);
[cell.contentView addSubview:tf];
return cell;
}
I assumed that by making the textfields frames origin (0,0) when i add this as a subview to the content view, it will place it in the content view at position (0,0), but this is not happening, it is actually placing it at position (0,0) of the entire cell (the top left most part of the cell).
Unless this has been changed in iOS7, i thought the content view was like this:
(source: idev101.com)
If so, is there a way i can actually place this in the contentView without having to manually define an offset each time?
Try This
if (cell==nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
UIImageView *img=[[UIImageView alloc]init];
img.tag=100;
img.layer.cornerRadius = 3;
img.layer.masksToBounds = YES;
[cell addSubview:img];
UILabel *lbl= [[UILabel alloc]init];
lbl.backgroundColor= [UIColor clearColor];
lbl.text=#"ABC";
lbl.textAlignment=NSTextAlignmentLeft;
lbl.textColor=[UIColor blackColor];
lbl.tag=200;
[cell addSubview:lbl];
}
img1=(UIImageView *)[cell viewWithTag:100];
NSArray *imgDataArr = [[NSArray alloc] initWithArray:[[arrFields objectAtIndex: storyIndex] objectForKey: #"image"]];
for (UIImage *image in imgDataArr)
{
if (image)
img1.image =image;
}
[cell addSubview:img1];
UILabel *lbl1 = (UILabel*)[cell viewWithTag:200];
[cell addSubview:lbl1];
lbl1.numberOfLines=3;
lbl1.text=[[arrFields objectAtIndex: storyIndex] objectForKey: #"Item_No"];
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";
As I scroll down the list, all the rows are there, but they keep adding more subviews the more they appear on the visible frame
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuse = #"RuleCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:reuse];
}
NSUInteger row = indexPath.row;
[self createCell: cell onRow: row];
return cell;
}
- (void) createCell: (UITableViewCell*)cell onRow: (NSUInteger)row
{
UIImageView* bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell_background_spade_active.png"]];
cell.backgroundView = bgImage;
cell.textLabel.hidden = YES;
UILabel* titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(100, CGRectGetHeight(cell.frame) / 2, 200, 50)];
titleLabel.text = [[self.ruleList objectAtIndex: row] objectForKey: TitleKey];
titleLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview: titleLabel];
}
I think you need to execute almost all of the logic that's in createCell: only within the if (cell == nil){ segment of your code. The part that should execute where you're currently calling createCell: is just getting a reference to the titleLabel and setting its text value.
To clarify, here's the kind of modification I'm suggesting (not tested, but should give the right idea):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuse = #"RuleCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:reuse];
[self setUpCell: cell];
}
NSUInteger row = indexPath.row;
UILabel *titleLabel = (UILabel *)[cell.contentView viewWithTag:42];
titleLabel.text = [[self.ruleList objectAtIndex: row] objectForKey: TitleKey];
return cell;
}
- (void) setUpCell: (UITableViewCell*)cell
{
UIImageView* bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell_background_spade_active.png"]];
cell.backgroundView = bgImage;
cell.textLabel.hidden = YES;
UILabel* titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(100, CGRectGetHeight(cell.frame) / 2, 200, 50)];
titleLabel.tag = 42;
titleLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview: titleLabel];
}