I am using a sliding view controller which holds the main controller on top and a slide left menu controller.
The controller on the left works as the menu like Facebook/Pintrest apps etc. It is a UITableView on the left.
Here is my setup:
cellForRowAtIndexPath
static NSString *CellIdentifier = #"MainMenuCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIView *topSplitterBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, 1)];
topSplitterBar.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:69.0/255.0 blue:85.0/255.0 alpha:1];
[cell.contentView addSubview:topSplitterBar];
UIImage *arrowImage = [UIImage imageNamed:#"selectedArrow"];
self.arrowSelectedView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 10, arrowImage.size.width, arrowImage.size.height)];
self.arrowSelectedView.image = arrowImage;
[cell.contentView addSubview:self.arrowSelectedView];
self.arrowSelectedView.hidden = YES;
}
//////
// ADDITIONAL GLUE CODE HERE TO SET LABELS ETC....
//////
if (currentDisplayed) {
// This is for the current item that is being displayed
cell.contentView.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1];
self.arrowSelectedView.hidden = NO;
} else {
cell.contentView.backgroundColor = [UIColor colorWithRed:75.0/255.0 green:83.0/255.0 blue:102.0/255.0 alpha:1.0];
self.arrowSelectedView.hidden = YES;
}
NSLog(#"%#",cell.contentView.subviews);
return cell;
There are a total of 7 cells in 2 sections for the table view. One cell in section 1 (0) and the rest in section 2 (1). There are three cells which show a main controller on the top. Once they are selected I would like to update the table view to show an arrow next the cell like this:
Here is the example code for: didSelectRowAtIndexPath
UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"ProfileVC"];
__weak typeof(self) weakSelf = self;
[self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^{
CGRect frame = weakSelf.slidingViewController.topViewController.view.frame;
weakSelf.slidingViewController.topViewController = newTopViewController;
weakSelf.slidingViewController.topViewController.view.frame = frame;
[weakSelf.slidingViewController resetTopViewWithAnimations:nil onComplete:^{
NSIndexPath* displayNameRow = [NSIndexPath indexPathForRow:0 inSection:0];
NSIndexPath* gamesRow = [NSIndexPath indexPathForRow:0 inSection:1];
NSIndexPath* settingsRow = [NSIndexPath indexPathForRow:3 inSection:1];
NSArray* rowsToReload = [NSArray arrayWithObjects:displayNameRow, gamesRow, settingsRow, nil];
[weakSelf.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
}];
}];
Issue/Question
So I have a strange issue here, were if I click on a different cell it works the arrow is shown on the other cell. Then again this works for 2 more times, then the third times it randomly decides to show the uiimageview on another cell.
Even if I step through the cell creation process I see for that specific cell (the incorrectly displayed one) the boolean for currentDisplayed is set to NO, so it doesn't change the arrowSelectedView to not hidden yet it does somehow? I log out the subviews and can see that it is randomly not Hidden anymore even though for that specific cell it is not set to not hidden, so I am thinking this is implemented incorrectly somehow?
The main issue here is that in -tableView:cellForRowAtIndexPath: you're creating a new image view every time you create a new cell, then storing it into a single ivar. The call to set or hide the image view later in that method is not guaranteed (indeed is actually quite unlikely) to be addressing the same image view that was added into that particular cell's content view.
A more convenient place to store it would be in a subclass of UITableViewCell's view like so:
#interface CustomTableViewCell : UITableViewCell
#property (strong, readwrite) UIImageView *imageAccessory;
#end
#implementation CustomTableViewCell
#end
#implementation YourClass
- (id)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MainMenuCell";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (nil == cell) {
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIView *topSplitterBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, 1)];
topSplitterBar.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:69.0/255.0 blue:85.0/255.0 alpha:1];
[cell.contentView addSubview:topSplitterBar];
UIImage *arrowImage = [UIImage imageNamed:#"selectedArrow"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 10, arrowImage.size.width, arrowImage.size.height)];
imageView.image = arrowImage;
cell.accessoryImage = imageView;
[cell.contentView addSubview: cell.accessoryImage];
cell.accessoryView.hidden = YES;
}
if (currentDisplayed) {
// This is for the current item that is being displayed
cell.contentView.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1];
cell.accessoryImage.hidden = NO;
} else {
cell.contentView.backgroundColor = [UIColor colorWithRed:75.0/255.0 green:83.0/255.0 blue:102.0/255.0 alpha:1.0];
cell.accessoryImage.hidden = YES;
}
return cell;
}
#end
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;}
I have a tableviewcontroller and a custom cell. What i wanna do is when i tap the cell, the cell is supposed to exapand and a view (graph view actually) is supposed to become subviewed inside the cell. Now the problem is that everything works fine but the graph is duplicated on some other cells as well.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ProductsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell == nil)
{
NSLog(#"empty cell");
}
//Product Label
cell.productNameLabel.text = #"something";
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
indexPathforChart = indexPath;
[self performSelector:#selector(addChart:) withObject:indexPath afterDelay:0.2];
[tableView beginUpdates];
[tableView endUpdates];
[tableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES];
}
-(void)addChart:(NSIndexPath*)indexPath
{
BEMSimpleLineGraphView *myGraph = [[BEMSimpleLineGraphView alloc] initWithFrame:CGRectMake(0, 60, screenSize.width, 200)];
myGraph.dataSource = self;
myGraph.delegate = self;
myGraph.interpolateNullValues = YES;
myGraph.enableTouchReport = YES;
myGraph.tag = 100;
myGraph.animationGraphStyle = BEMLineAnimationDraw;
myGraph.enablePopUpReport = YES;
myGraph.enableXAxisLabel = YES;
myGraph.colorXaxisLabel = [UIColor darkGrayColor];
ProductsTableViewCell *cell = (ProductsTableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];
[cell.contentView addSubview:myGraph];
[cell setNeedsLayout];
[cell setNeedsDisplay];
myGraph.colorTop = [UIColor clearColor];
myGraph.colorBottom = [UIColor clearColor];
myGraph.colorLine = [UIColor darkGrayColor];
myGraph.colorPoint = [UIColor lightGrayColor];
}
This is caused by cell re-use.
ProductsTableViewCell *cell = (ProductsTableViewCell*)[self.tableView
cellForRowAtIndexPath:indexPath];
[cell.contentView addSubview:myGraph];
You added myGraph as a subview in the cell without removing it when the cell is re-used by some other index path while you scroll the table view.
The most appropriate way should be having a custom view inside the cell for drawing your graph, instead of adding/removing the graph view when needed. For the sake of scrolling performance, you may also cache the graph in case it will be used when user scrolls back and forth.
Cells are reused, so before loading a new cell you should implement the method prepareForReuse and add/remove or hidden/unhidden the views your cell requires.
So basically, ProductsTableViewCell should implement the method prepareForReuse. The easiest way to remove your BEMSimpleLineGraphView based on your code would be:
- (void) prepareForReuse{
UIView *v = [cell.contentView viewWithTag:100];
if ( v ) {
[v removeFromSuperView];
}
}
However, I don't consider using viewWithTag is the best solution so I would change the code into something similar to:
tableviewcontroller
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ProductsTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[tableView beginUpdates];
[cell addChart];
[tableView endUpdates];
[tableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES];
}
ProductsTableViewCell
#interface DLSContactUsViewController ()
#property (strong,nonatomic) BEMSimpleLineGraphView *myGraph;
#end
-(void)addChart
{
if ( ![self.myGraph isDescendantOfView] ){
[self.contentView addSubview:self.myGraph];
[self setNeedsLayout];
[self setNeedsDisplay];
}
}
- (BEMSimpleLineGraphView*) myGraph{
if ( !_myGraph ) {
_myGraph = [[BEMSimpleLineGraphView alloc] initWithFrame:CGRectMake(0, 60, screenSize.width, 200)];
_myGraph.dataSource = self;
_myGraph.delegate = self;
_myGraph.interpolateNullValues = YES;
_myGraph.enableTouchReport = YES;
_myGraph.tag = 100;
_myGraph.animationGraphStyle = BEMLineAnimationDraw;
_myGraph.enablePopUpReport = YES;
_myGraph.enableXAxisLabel = YES;
_myGraph.colorXaxisLabel = [UIColor darkGrayColor];
_myGraph.colorTop = [UIColor clearColor];
_myGraph.colorBottom = [UIColor clearColor];
_myGraph.colorLine = [UIColor darkGrayColor];
_myGraph.colorPoint = [UIColor lightGrayColor];
}
return _myGraph;
}
- (void) prepareForReuse{
if ( [self.myGraph isDescendantOfView] && !self.isSelected ) {
[myGraph removeFromSuperView];
}
}
I'm trying to display an image in only certain cells in my UITableView. Here's a my configureCell method:
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
StoryInfo *info = [self.fetchedResultsController objectAtIndexPath:indexPath];
UIImage *ribbon = [UIImage imageNamed:#"ribbon.png"];
UIImageView *ribbonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
[ribbonView setImage:ribbon];
[cell addSubview:ribbonView];
if([[NSNumber numberWithBool:NO] isEqualToNumber:info.visited]) {
cell.textLabel.textColor = [UIColor colorWithRed:53/255.0 green:53/255.0 blue:52/255.0 alpha:1];
ribbonView.hidden = NO;
}
else {
cell.textLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
ribbonView.hidden = YES;
}
}
And here's cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// set up the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
This doesn't quite work because at first, all of the cells draw them ribbonView, regardless of the value of info.visited. I've stepped through the if/else and I see that the hiding code it being hit, though. But, if I navigate away from the list, and then come back, the correct ribbon state is visible. Scrolling the table view breaks it again.
The font colors are always correct, though.
If you are reusing cells, then it could likely be that you are adding multiple ribbonView subviews to the cell, so even if the info.visited for the current indexPath is NO, there is another ribbonView leftover on the cell that you can still see.
The thing to do is make certain you only ever have one ribbonView subview, which can be done either by removing old ribbonViews in your configuration method, or better by subclassing UITableViewCell and adding a ribbonView property to the cell, which gets set once and added to the cell's view hierarchy once, and which you can then access and set hidden to NO or YES in the configuration method.
EDIT: The cell text color will always be correct since you are changing the color on one instance of UILabel that is in the cell's view hierarchy. I expect you'd see the same buggy behavior if instead your configuration method added a new UILabel subview to the cell each time it was configured.
EDIT: Code to try
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
static NSInteger ribbonTag = 12345;
StoryInfo *info = [self.fetchedResultsController objectAtIndexPath:indexPath];
// re-use a ribbonView if one's already been added to this cell
UIImageView *ribbonView = [cell.contentView viewWithTag:ribbonTag];
if (!ribbonView){
ribbonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
ribbonView.tag = ribbonTag;
UIImage *ribbon = [UIImage imageNamed:#"ribbon.png"];
[ribbonView setImage:ribbon];
// add subviews to contentView
[cell.contentView addSubview:ribbonView];
}
if([[NSNumber numberWithBool:NO] isEqualToNumber:info.visited]) {
cell.textLabel.textColor = [UIColor colorWithRed:53/255.0 green:53/255.0 blue:52/255.0 alpha:1];
ribbonView.hidden = NO;
}
else {
cell.textLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
ribbonView.hidden = YES;
}
}
If you are using dequeueReusableCellWithIdentifier:forIndexPath: in the tableView:cellForRowAtIndexPath: method. Every time you reutilize a cell, you will create a new UIImageView and put it over the last one.
But to solve this you dont need to subclass. Not yet, because your cells are simple still. If you want to add more subviews, then subclassing is the only option.
One solution I can think of would be this:
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
StoryInfo *info = [self.fetchedResultsController objectAtIndexPath:indexPath];
UIImageView *ribbonView = nil;
//My code:
for ( UIView *childView in cell.subviews ) {
if([childView isKindOfClass:[UIImageView class]] {
ribbonView = childView;
break;
}
}
//Note: this doesnt work if you have more than one UIImageView in your cell.
if(ribbonView == nil) {
UIImage *ribbon = [UIImage imageNamed:#"ribbon.png"];
ribbonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
[ribbonView setImage:ribbon];
[cell addSubview:ribbonView];
}
//Ends here.
if([[NSNumber numberWithBool:NO] isEqualToNumber:info.visited]) {
cell.textLabel.textColor = [UIColor colorWithRed:53/255.0 green:53/255.0 blue:52/255.0 alpha:1];
ribbonView.hidden = NO;
}
else {
cell.textLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
ribbonView.hidden = YES;
}
}
Try it and tell me if it works.
Good luck.
That is definitely an issue in implementation of
tableView:cellForRowAtIndexPath:
You need to always call
dequeueReusableCellWithIdentifier:forIndexPath:
and then call your configure method
configureCell:atIndexPath:(NSIndexPath *)indexPath
EDIT:
Also, I think you also need to do [cell.contentView addSubView:...] instead of [cell addSubView:...]
I want to display an image over a cell upon selection. I've tried endless ways to do so without succes.
I've tried adding this to didSelectRowAtIndexPath:
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.imageView.image = [UIImage imageNamed:#"deselected_image.png"];
cell.imageView.highlightedImage = [UIImage imageNamed:#"selected_image.png"];
But the image won't change upon push.
Thanks
The code you have is creating a new table view cell - you need to retrieve the current cell that was selected. Try the following instead:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:#"deselected_image.png"];
cell.imageView.highlightedImage = [UIImage imageNamed:#"selected_image.png"];
}
I have applied the UIColor to each cell on request.You can replace the UIColor code & apply the image.
Here is my code
- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
{
if(tableView==tblLanguage)
{
cell.backgroundView = [[UIView alloc] init];
((UIView *)cell.backgroundView).backgroundColor = !(indexPath.row % 2)?[UIColor colorWithRed:(float)231/255 green:(float)231/255 blue:(float)231/255 alpha:1.0]:[UIColor colorWithRed:(float)218/255 green:(float)218/255 blue:(float)218/255 alpha:1.0];
((UIView *)cell.backgroundView).alpha=1.0;
//ilangSelectedIndex is set in the didselect row of the tableview delegate function
if(ilangSelectedIndex==[indexPath row])
{
((UIView *)cell.backgroundView).backgroundColor=[UIColor colorWithRed:(float)171/255 green:(float)177/255 blue:(float)213/255 alpha:1.0];
}
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}
}
In the Did select function of the table you set the ilangSelectedIndex as Indexpath.row of your table & reload the table to see the effect.
Try this code in didSelectRowAtIndexPath:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selectedBackgroundView=[[UIImageView alloc]init];
cell.backgroundView =[[UIImageView alloc] init];
((UIImageView *)cell.selectedBackgroundView).image=[UIImage imageNamed:#"selected.png"];
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.