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";
Related
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 = ...
}
...
Hi i have one button in UIView. My requirement is if i click that button UITextFields displayed in UITableViewCells. I am having idea how to display the UITextFields in UIView if user clicks the button. But i dont have any idea how to display UITextFields inside of UITableViewCells if user hits the button.Please help me anybody.
UIButton *myGreenIconButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[myGreenIconButton1 addTarget:self action:#selector(GreenIconButtonClicked)forControlEvents:UIControlEventTouchUpInside];
[myGreenIconButton1 setBackgroundImage:[UIImage imageNamed:#"index.jpg"] forState:UIControlStateNormal];
myGreenIconButton1.backgroundColor = [UIColor clearColor];
myGreenIconButton1.frame = CGRectMake(285, 144, 25, 25);
[self.view addSubview:myGreenIconButton1];
-(void)GreenIconButtonClicked
{
UITextField *text1=[[UITextField alloc]initWithFrame:CGRectMake(10, 80, 100, 20)];
text1.borderStyle=UITextBorderStyleRoundedRect;
text1.backgroundColor=[UIColor clearColor];
text1.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
text1.font=[UIFont systemFontOfSize:14.0];
text1.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
text1.textAlignment=NSTextAlignmentCenter;
text1.delegate=self;
[self.view addSubview:text1];
UITextField *text2=[[UITextField alloc]initWithFrame:CGRectMake(120, 80, 100, 20)];
text2.borderStyle=UITextBorderStyleRoundedRect;
text2.backgroundColor=[UIColor clearColor];
text2.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
text2.font=[UIFont systemFontOfSize:14.0];
text2.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
text2.textAlignment=NSTextAlignmentCenter;
text2.delegate=self;
[self.view addSubview:text2];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
[cell.textLabel setText:#""];
return cell;
}
Try use this:
[yourSubview setTag:101];
[cell.contentView addSubview:yourSub];
in cellForRowAtIndexPath method
And you can restore your textField like this:
NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:ip]
UITextField *textField = (UITextField*)[cell viewWithTag:101];
Pay attention to 101 - it is number you can use for tag. And different views (for ex: cell.contentView) may contain equal tags.
You need to subclass UITableViewCell and add in an outlet for your textfield. Lot of examples out there if you look for them. Here is one:
http://mobile.tutsplus.com/tutorials/iphone/customizing-uitableview-cell/
Something like this (MyCustomTableViewCell.h):
#import <UIKit/UIKit.h>
#interface MyCustomTableViewCell : UITableViewCell
#property(nonatomic,weak) IBOutlet UITextField *myTextField;
#end
And then in the cellForRowAtIndexPath:
static NSString *CellIdentifier = #"Cell";
MyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.myTextField.text = #"Whatever";
Don't forget to #import "MyCustomTableViewCell.h" and also wire up that textfield in IB
In the comment controller of an app I am creating I have two tableViews: The comment table and the table that appears when trying to tag another user.
All data is loading properly, but when I attempt to create a custom cell (ExploreCell) my custom properties (the UILabel and UIImageView) are not displaying. For testing's sake I tried chaining the background colors of the cells and they do set to black.
I have registered all of the proper nibs and I can't figure out why this is happening.
-(UITableViewCell *)tableView:(UITableView *)tableViewItem cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"cellforrow");
if (tableViewItem == self.tableView) {
static NSString *identifier = #"Cell";
CommentCell *cell = (CommentCell *)[tableViewItem dequeueReusableCellWithIdentifier:identifier];
if (fetched == YES) {
[cell.usernameLabel setTitle:[[[commentArray objectAtIndex:indexPath.row]objectForKey:#"user"] valueForKey:#"username"] forState:UIControlStateNormal];
cell.usernameLabel.tag = indexPath.row;
[cell.usernameLabel addTarget:self action:#selector(goToUser:) forControlEvents:UIControlEventTouchUpInside];
[cell.profilePicture setImage:[profPicDictionary objectForKey:[[[commentArray objectAtIndex:indexPath.row]objectForKey:#"user"] valueForKey:#"username"]]];
}
[cell.commentLabel setUserInteractionEnabled:YES];
[cell.commentLabel setScrollEnabled:NO];
[cell.commentLabel setEditable:NO];
[cell.commentLabel setSelectable:YES];
[cell.commentLabel setDelegate:self];
cell.commentLabel.attributedText = [self getAttributedText:[[commentArray objectAtIndex:indexPath.row]valueForKey:#"comment"]];
cell.commentLabel.contentInset = UIEdgeInsetsMake(0, -5, 0, 0);
[cell.commentLabel setFrame:CGRectMake(cell.commentLabel.frame.origin.x, cell.commentLabel.frame.origin.y, 257, 10)];
[cell.commentLabel sizeToFit];
NSDate *createdAt = [[commentArray objectAtIndex:indexPath.row]valueForKey:#"createdAt"];
cell.createdLabel.text = [self setCreatedTimestamp:createdAt];
[cell.usernameLabel.titleLabel setFont:[UIFont fontWithName:#"Radikal-Thin" size:14.0f]];
[cell.commentLabel setFont:[UIFont fontWithName:#"Radikal-Thin" size:15.0f]];
[cell.createdLabel setFont:[UIFont fontWithName:#"Radikal-Thin" size:14.0f]];
return cell;
} else {
NSLog(#"explore");
static NSString *identifier = #"userCell";
ExploreCell *tagCell = (ExploreCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (!tagCell) {
tagCell = [[ExploreCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"userCell"];
}
if (tableData.count >> 0) {
[tagCell.usernameLabel setText:[[tableData objectAtIndex:indexPath.row]valueForKey:#"username"]];
[tagCell.profilePicture setImage:[profPicDictionary objectForKey:[[tableData objectAtIndex:indexPath.row]valueForKey:#"username"]]];
tagCell.backgroundColor = [UIColor blackColor];
}
NSLog(#"tagcell: %#",tagCell);
return tagCell;
}
}
Edit the following line in else part and you should be good to go
ExploreCell *tagCell = (ExploreCell *)[tableViewItem dequeueReusableCellWithIdentifier:identifier];
Change this line and let me know if you still find any problem.
So I am having trouble showing my data in a UITableView. I do believe it has something to do with reusing the cells. I have checked online and here at SO but have not found a solution that works for me. Any help would be appreciated.
I have an Array that is populated by text and pictures. I am then showing the information in a tableView. If I were to use static sized cells everything works out fine, but the amount of text changes, so I have also implemented the heightForRowAtIndexPath method. This works as well, until I scroll all the way down to the bottom.
After that, when I scroll back up, all the cell heights change and the display gets all jumbled. Some text gets cut off, pictures get chopped and some of the cells only have the last portion of text. I really think it has something to do with reusing the cells, but I don’t know how to attack this problem. Below is my code for cellForRowAtIndexPath and heightForRowAtIndexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([[_theBigArray objectAtIndex:indexPath.row] isKindOfClass:[NSString class]])
{
NSString *label = [_theBigArray objectAtIndex:indexPath.row];
CGSize stringSize = [label sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:NSLineBreakByWordWrapping];
UITextView *textV = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 290, stringSize.height +50)];
textV.font = [UIFont systemFontOfSize:15];
textV.text = [_theBigArray objectAtIndex:indexPath.row];
textV.textColor = [UIColor blackColor];
textV.editable = NO;
[cell.contentView addSubview:textV];
}
else if ([[_theBigArray objectAtIndex:indexPath.row] isKindOfClass:[UIImage class]])
{
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 290, 100)];
imageV.contentMode = UIViewContentModeScaleAspectFit;
imageV.image = [_theBigArray objectAtIndex:indexPath.row];
[cell.contentView addSubview:imageV];
}
return cell;
[tableView reloadData];
}
For heightForRowAtIndexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int rowHeight = 0.0f;
if ([[_theBigArray objectAtIndex:indexPath.row] isKindOfClass:[NSString class]])
{
NSString *temp = [_theBigArray objectAtIndex:indexPath.row];
CGSize size = [temp sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:NSLineBreakByWordWrapping];
rowHeight = size.height+50;
}
else if ([[_theBigArray objectAtIndex:indexPath.row] isKindOfClass:[UIImage class]])
{
rowHeight = 115.0f;
}
//NSLog(#"rowHeight is %i", rowHeight);
return rowHeight;
[tableView reloadData];
}
I even tried to make two different cells and call them separately, but the same thing happens. I did still use the same heightForRowAtIndexPath method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *newCell = [[UITableViewCell alloc] init];
if ([[_theBigArray objectAtIndex:indexPath.row] isKindOfClass:[NSString class]])
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
NSString *label = [_theBigArray objectAtIndex:indexPath.row];
CGSize stringSize = [label sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:NSLineBreakByWordWrapping];
UITextView *textV = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 290, stringSize.height +50)];
textV.font = [UIFont systemFontOfSize:15];
textV.text = [_theBigArray objectAtIndex:indexPath.row];
textV.textColor = [UIColor blackColor];
textV.editable = NO;
[cell.contentView addSubview:textV];
newCell = cell;
}
else if ([[_theBigArray objectAtIndex:indexPath.row] isKindOfClass:[UIImage class]])
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"PictureCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"PictureCell"];
}
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 290, 100)];
imageV.contentMode = UIViewContentModeScaleAspectFit;
imageV.image = [_theBigArray objectAtIndex:indexPath.row];
[cell.contentView addSubview:imageV];
newCell = cell;
}
return newCell;
[tableView reloadData];
}
Any ideas?
The main problem is that you're adding subviews to cells every time they scroll in, but when a cell is reused, it will already have those subviews added. (That is, when a cell is reused, it will already have a UITextView or UIImageView depending on the reuse identifier.)
You need to check if these subviews exist first; this is commonly done by using the -[UIView viewWithTag] method, or by subclassing UITableViewCell and assigning each view as a property.
(You can take a look at the SO question How to get other control value from UITableViewCell? to see how to use viewWithTag. I would avoid subclassing UITableViewCell until you're more comfortable with the out-of-the-box implementation.)
Also, this line of code:
UITableViewCell *newCell = [[UITableViewCell alloc] init];
is a terrible idea, because you are creating a new UITableViewCell without checking to see if you can reuse one first. This defeats the entire purpose of reusing cells, which is fast scrolling performance. Instead, just declare it without initializing it:
UITableViewCell *newCell;
Also, in heightForRowAtIndexPath, you are
declaring rowHeight as an int (it should be a CGFloat)
trying to call reloadData after the method returns (which will never happen, but you should never try to call reloadData from this method)
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.