overlapping subviews in UITableViewCell - iphone

I am stuck with this. I will explain with my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSNumber *cellno=[NSNumber numberWithUnsignedInteger:indexPath.row];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 13, 15,18)];
imgView.image=[UIImage imageNamed:#"lock.png"];
tickView = [[UIImageView alloc] initWithFrame:CGRectMake(200, 13, 15,18)];
tickView.image=[UIImage imageNamed:#"tick.png"];
switch (indexPath.row) {
case 0:
cell.textLabel.text=#"apples";
if ([appDelegate.connected containsObject:cellno]) { //condition
[cell.contentView addSubview:tickView];
}else{
[cell.contentView addSubview:imgView];
}
break;
}
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
During the first time the tableview is loaded the 'imgView' subview is added to the cells content view and the after some operation the 'if' condition is satisfied and the 'tickView' is added.
The problem is, the oldview is not hidden or removed and hence both the images appear.
Help would be greatly appreciated

Instead of creating your imgView and tickView views in the cellForRowAtIndexPath method, create them when you create the cell and reuse them with the cell. Then you can do this:
...
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 13, 15,18)];
imgView.image=[UIImage imageNamed:#"lock.png"];
tickView = [[UIImageView alloc] initWithFrame:CGRectMake(200, 13, 15,18)];
tickView.image=[UIImage imageNamed:#"tick.png"];
}
...
if ([appDelegate.connected containsObject:cellno]) { //condition
[imgView removeFromSuperview];
[cell.contentView addSubview:tickView];
}else{
[tickView removeFromSuperview];
[cell.contentView addSubview:imgView];
}

Related

TableviewCell change unexpectablly

I have created an app that contain tableview now i am setting background of that cell and add a view for separator. it look fine but when tebleview scroll than my separator disappear. like this,
First time
When table scroll
this is my code for adding tableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *SimpleTableIdentifier;
UITableViewCell * cell;
SimpleTableIdentifier = #"SimpleTableIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier: nil];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
// Configure the cell...
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor colorWithRed:(224/255.0) green:(230/255.0) blue:(241/255.0) alpha:1];
[cell setBackgroundView:bgview];
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 1)];
separatorLineView.backgroundColor = [UIColor colorWithRed:(74/255.0) green:(89/255.0) blue:(138/255.0) alpha:1];// you can also put image here
[cell.contentView addSubview:separatorLineView];
}
return cell;
}
USE
cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
Try
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *SimpleTableIdentifier;
UITableViewCell * cell;
SimpleTableIdentifier = #"SimpleTableIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 42, 320, 1)];
[separatorLineView setTag:1];
[cell.contentView addSubview:separatorLineView];
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor colorWithRed:(224/255.0) green:(230/255.0) blue:(241/255.0) alpha:1];
[cell setBackgroundView:bgview];
}
UIView *SPview=[cell viewWithTag:1];
SPview.backgroundColor = [UIColor blueColor];// you can also put image here
return cell;
}
The problem is the frame you set for the view I changed it from 44 to 42 for the seperator line orgin and now it works fine
Move the configuring of the cell outside of
if (cell == nil) {
cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
}
// everything else
should do the trick.
Use it
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *SimpleTableIdentifier;
UITableViewCell * cell;
SimpleTableIdentifier = #"SimpleTableIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier: nil];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
// Configure the cell...
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor colorWithRed:(224/255.0) green:(230/255.0) blue:(241/255.0) alpha:1];
[cell setBackgroundView:bgview];
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 1)];
separatorLineView.backgroundColor = [UIColor colorWithRed:(74/255.0) green:(89/255.0) blue:(138/255.0) alpha:1];// you can also put image here
[cell.contentView addSubview:separatorLineView];
return cell;
}
Your problem is you're not configuring your cell each time you get a new one. You should populate your cell each time cellForRowAtIndexPath is called.
Try changing your code to:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *SimpleTableIdentifier;
UITableViewCell * cell;
SimpleTableIdentifier = #"SimpleTableIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier: nil];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
// Configure the cell...
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor colorWithRed:(224/255.0) green:(230/255.0) blue:(241/255.0) alpha:1];
[cell setBackgroundView:bgview];
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 1)];
separatorLineView.backgroundColor = [UIColor colorWithRed:(74/255.0) green:(89/255.0) blue:(138/255.0) alpha:1];// you can also put image here
[cell.contentView addSubview:separatorLineView];
return cell;
}
Good answer at here.
First add separator thru the xib file and than put this code in cellForRowAtIndexPath.
tableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 10.0f)] autorelease];

How to control my tableview cell label?

i am new in iPhone development. I am developing an app which contains tableview with label inside the cell. I have placed different label for each row like this if(indexpath.row == 0) like this for each cell row i have done it. But when i am scrolling the tableview my label are getting mixed. Please help me!
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.row == 2)
{
pendingListNumberLabel = [[UILabel alloc] init];
pendingListNumberLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
pendingListNumberLabel.textAlignment = UITextAlignmentLeft ;
pendingListNumberLabel.textColor = [UIColor orangeColor];
pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30);
[pendingListNumberLabel setBackgroundColor:[UIColor clearColor]];
[cell addSubview:pendingListNumberLabel];
}
cell.textLabel.text = [affiliationsArray objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
You are adding the UILabel as a subview, that is the mistake here.
Normally you would use a custom UITableViewCell for this kind of stuff, but a quick fix is this:
if (indexPath.row == 2)
{
pendingListNumberLabel = [[UILabel alloc] init];
pendingListNumberLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
pendingListNumberLabel.textAlignment = UITextAlignmentLeft ;
pendingListNumberLabel.textColor = [UIColor orangeColor];
pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30);
[pendingListNumberLabel setBackgroundColor:[UIColor clearColor]];
pendingListNumberLabel.tag = 1337
[cell addSubview:pendingListNumberLabel];
} else {
for (UIView * subView in [cell subviews]) {
if ([subView tag] == 1337) {
[subView removeFromSuperview];
}
}
}
This is happening because you are using reusable cells and not in a right way ..
just replace your code -
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
with this -
UITableViewCell *cell = [[UITableViewCell alloc] init];
the above code can solve your issue, but this is not good if there are a large number of cells. In that case you should use reusable cells.
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
else
{
UILabel *titleLbl = (UILabel *)[cell viewWithTag:1];
[titleLbl removeFromSuperview];
}
if (indexPath.row == 2)
{
pendingListNumberLabel = [[UILabel alloc] init];
pendingListNumberLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
pendingListNumberLabel.textAlignment = UITextAlignmentLeft ;
pendingListNumberLabel.textColor = [UIColor orangeColor];
pendingListNumberLabel.tag = 1;// Write here.....................................
pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30);
[pendingListNumberLabel setBackgroundColor:[UIColor clearColor]];
[cell addSubview:pendingListNumberLabel];
}
cell.textLabel.text = [affiliationsArray objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
I think it will be helpful to you.
You can fix the problem of label mixing just by omitting the if (cell == nil) checking. Use the bellow code:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//if (cell == nil) // Need not this checking
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
Use the cell allocation directly as above :)
In your tableView:cellForRowAtIndexPath: method, first get the selected row like this
NSUInteger row = [indexPath row];
then use "row" to grab your data from an NSArray that corresponds to the tableView rows and assign it to your cell's textLabel's text property like this
NSString *string = [NSString stringWithFormat:#"%#",self.allItems objectAtIndex:row];
cell.textLabel.text = string;
return cell;
And of course you would have already created a UITableViewCell with a Reuse identifier like this :
static NSString *TableIdentifier = #"TableIdentifier";
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:TableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:TableIdentifier];
}
The key is using an array that corresponds exactly to your table rows. (ie - item 0 in the Array corresponds to row 0 in the tableView) then you can't go wrong.
For solve this issue you can put cell=nil; before checking cell== nil condition or create cell every time. It will work but it is not good Habit.
This also work for Text
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.row == 2)
{
pendingListNumberLabel = [[UILabel alloc] init];
pendingListNumberLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
pendingListNumberLabel.textAlignment = UITextAlignmentLeft ;
pendingListNumberLabel.textColor = [UIColor orangeColor];
pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30);
[pendingListNumberLabel setBackgroundColor:[UIColor clearColor]];
[cell addSubview:pendingListNumberLabel];
}
cell.textLabel.text = [affiliationsArray objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;

Enhance UITableView Scrolling

in my app i have an UITableView with 120 rows and every row has 1 UItextfeilds and 1 Buttons as show in the code bellow :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
else
{
}
UITextField *NameTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 10, 230, 28)];
NameTextField.borderStyle = UITextBorderStyleRoundedRect;
NameTextField.delegate = self;
NameTextField.textColor = UIColorFromRGB(0x2A1807);
NameTextField.font = [UIFont fontWithName:#"Helvetica" size:(17.0)];
NameTextField.font = [UIFont boldSystemFontOfSize:20];
NameTextField.tag = [indexPath row ];
NSString *temp = [self.sectionNames objectAtIndex:[indexPath section]];
NameTextField.text = [[self.myDataArray objectForKey:temp] objectAtIndex:[indexPath row]];
[cell.contentView addSubview:NameTextField];
[NameTextField release];
UIButton * aBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *wijzigImage = [UIImage imageNamed:#"btn_delete.png"];
aBtn.frame = CGRectMake(240, 10, 28, 26);
[aBtn setImage:wijzigImage forState:UIControlStateNormal];
[aBtn addTarget:self action:#selector(deleteCustomCellWithUIButton:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:aBtn];
return cell;
}
i have noticed that the Scrolling is slow and It doesn't go fluently.
any idea ?
Thanks
That's because you create textfields and buttons every time, add it inside if (cell == nil) {...}. The only thing that should be left outside that if is textField text setting.
Btw, your textfield leaks.
I found the solution :
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
else
{
UITextField *oldTextField = (UITextField *)[cell.contentView viewWithTag:999];
[oldTextField removeFromSuperview];
UIButton *oldBtn = (UIButton *)[cell.contentView viewWithTag:888];
[oldBtn removeFromSuperview];
}
NameTextField.tag = 999;
aBtn.tag = 88;

UITableView Custom View and "Load More" in the same cell

I have UITableView with Custom View on it.
For the bottom of the tableview, I add a "Load More" cell there.
After clicked, the tableview load more data successfully and the "Load More" cell still at the bottom of the tableview.
However, after I clicked the first "Load More", the second "Load More" appears, and the custom view still exist in the same cell of second "Load More". "Load More" and custom view are on the same cell. I want that cell only appear "Load More".
This problem exists for the third, fourth "Load More".
Can anyone help me?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return imageCurPos+1;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([aTableView tag]==501){
// Main Table
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
[[cell viewWithTag:3007] removeFromSuperview];
const NSInteger BOTTOM_LABEL_TAG = 3002;
UIImageView *bottomLabel;
UILabel *loadMore;
if(indexPath.row < imageCurPos){
if (cell == nil)
{ //All repeat things come here, if don't want to repeat here, please state outside this brucket
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//Edited
for (UIView *view in [cell.contentView subviews])
{
[view removeFromSuperview];
}
bottomLabel = [[[UIImageView alloc] initWithFrame: CGRectMake(50, 30, 250, 112)] autorelease];
[cell.contentView addSubview:bottomLabel];
bottomLabel.tag = BOTTOM_LABEL_TAG;
cell.backgroundView =
[[[UIImageView alloc] init] autorelease];
cell.selectedBackgroundView =
[[[UIImageView alloc] init] autorelease];
UIImageView *iconView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 30, 45, 45)] autorelease];
iconView.image = [UIImage imageNamed:#"02_bubble.png"];
[[cell contentView] addSubview:iconView];
bottomLabel.image = [UIImage imageNamed:#"05_bubble.png"];
}else if(indexPath.row == imageCurPos ){ //For Load More
if (cell == nil)
{ //All repeat things come here, if don't want to repeat here, please state outside this brucket
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//Edited
for (UIView *view in [cell.contentView subviews])
{
[view removeFromSuperview];
}
loadMore =
[[[UILabel alloc]
initWithFrame:
CGRectMake( 0, 0, 300, 50)]
autorelease];
loadMore.text = #"Load more...";
loadMore.textAlignment = UITextAlignmentCenter;
loadMore.tag = 3007;
loadMore.textColor = [UIColor whiteColor];
loadMore.backgroundColor = [UIColor darkTextColor];
[cell.contentView addSubview:loadMore];
}
return cell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath{
if([tableView tag]==501){
if([indexPath row] != imageCurPos){
}else{ // For load more
NSLog(#"noRow Prev: %d", imageCurPos);
imageCurPos += interval;
NSLog(#"noRow After: %d", imageCurPos);
[tbl_mo_main reloadData];
}
}
I think you should use 2 different identifiers, one for the normal cell and one for load more cell.
And one thing is that creating custom cell should be done in the block.
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// create the views in side the cell and add it
...
}
Then outside this block, update the cell.
You can set the tag for your views in the creation of cell, and update the corresponding view by getting it with [cell.contentView viewWithTag:tag]
You may refer to this topic: Reload TableViewCell's UILabels in dynamic table
[cell.contentView addSubview:loadMore];
This line adds the label to cell contentview but when the cell is reused ..that label is still present in that cell.
Add the following code after if(cell == nil) in both if and else for index path checking.
for (UIView *view in [cell.contentView subviews])
{
[view removeFromSuperview];
}

UITableView problem with adding extra cell

First question on this site, although I have been around for a while behind the scenes. I have a problem that I have been racking my head on for the last two days and I hope someone can shed some light on it for me.
I have a UITableView, which is loaded from a SQL database. It has 15 entries in it. I have inserted an extra cell at the beginning of the UITableView. This extra cell is for a UITextField and UIButton which adds an item into the database.
When the view is loaded, the first cell with the custom objects shows fine, and the rest of the table is filled with items from the database and looks just how it should. However, when the UITableView is scrolled down so the first cell is out of view, then back up, it takes the value of the 11th row item and shows it over top the first cell.
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)popTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
static NSInteger NameTag = 1;
UITableViewCell *cell = [popTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
CGRect frame;
frame.origin.x =50;
frame.origin.y =10;
frame.size.height =22;
frame.size.width =275;
UILabel *nameLabel = [[UILabel alloc] initWithFrame:frame];
nameLabel.tag = NameTag;
nameLabel.opaque = YES;
nameLabel.textColor = [UIColor grayColor];
nameLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:nameLabel];
[nameLabel release];
}
int row = [indexPath row];
if (row == 0) {
UIButton *buttonLeft = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonLeft setFrame: CGRectMake( 205, 6, 40, 33)];
[buttonLeft addTarget:self action:#selector(addToList:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:buttonLeft];
//No Alloc for txtField, it is built in IB
[txtField setBorderStyle:UITextBorderStyleNone];
[txtField setFrame: CGRectMake( 17, 12, 180, 23)];
txtField.backgroundColor = [UIColor clearColor];
[txtField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
txtField.clearButtonMode = UITextFieldViewModeAlways;
}else{
UILabel * nameLabel = (UILabel *) [cell.contentView viewWithTag:NameTag];
Add *theObj = [self.theArray objectAtIndex:indexPath.row - 1];
[nameLabel setText:theObj.itemName];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UIImageView *imageView = [cell viewWithTag:kTagCellImageView];
if (imageView == nil) {
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10.0, 10.0, 13.0, 25.0)];
imageView.backgroundColor = [UIColor clearColor];
imageView.tag = kTagCellImageView;
[cell.contentView addSubview:imageView];
}
if([theObj.itemName isEqualToString:#"First Street"]){
imageView.frame = CGRectMake(14,10,13,25);
[imageView setImage:[UIImage imageNamed:#"firststreet"]];
}
else if([theObj.itemName isEqualToString:#"Second Street"]){
imageView.frame = CGRectMake(8,12,29,20);
[imageView setImage:[UIImage imageNamed:#"second"]];
}
else if([theObj.itemName isEqualToString:#"Main Street"]){
imageView.frame = CGRectMake(15,10,13,25);
[imageView setImage:[UIImage imageNamed:#"mainstreet"]];
}
else{
imageView.frame = CGRectMake(8,8,25,25);
[imageView setImage:[UIImage imageNamed:#"iconcustom"]];
}
NSLog(#"%#",itemName);
NSLog(#"%#",itemCategory);
}
return cell;
}
Also here is my cellForRow:
- (NSInteger)tableView:(UITableView *)popTableView numberOfRowsInSection:(NSInteger)section {
return [self.theArray count] + 1; //Add Extra cell to beginning
}
Any ideas would be greatly appreciated.
You need to use a different reuseIdentifier for your first cell. Try this:
NSString *cellIdentifier;
if (indexPath.row == 0) {
cellIdentifier = #"first";
} else {
cellIdentifier = #"not first";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
// .. cell initialization
}
Obligatory tangential answer - have you thought about setting the tableHeaderView on the UITableView instead? I think that'd accomplish what you're trying to do in a cleaner way (in that it adds an arbitrary view to the top of the table).
Just create a UIView with your "add a new item" controls in it and then set it as the header view when first creating the table.
The issue is here
static NSString *CellIdentifier = #"Cell";
static NSInteger NameTag = 1;
UITableViewCell *cell = [popTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
you are dequeueing all of the cells with the same identifier. Row 1 (index 0) needs to have its own CellIdentifier. Also it looks like you keep adding subviews to the same cells that you dequeue. On your if(cell == nil) check you may want to decide if you want to remove all of the cells contentView subviews or reuse them.