UITableView cell labels moving around when scrolling - objective-c

I created a uitableview and when i launch it, it looks as it is supposed to, but when I scroll, it puts text in all the other sections that I don't specify, can someone please help.
The first image attached is how it should look. The second it what it does when I scroll.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0 ){
return 1;}
else if (section == 1){
return [cellLabels count];
}else if (section == 2){
return 1;
}else{
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
tableView.showsVerticalScrollIndicator = NO;
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
if( indexPath.section == 0 )
{
}
else if (indexPath.section == 1)
{
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor blackColor];
// headerLabel.font = [UIFont SystemFontOfSize:16];
[cell.textLabel setFont:[UIFont fontWithName:#"Arial" size:14]];
cell.textLabel.text = [cellLabels objectAtIndex:indexPath.row];
}
return cell;
}

your problem is pretty simple ,
since table view reuses allocated cells,
when it comes to first time to first section your displaying nothing , in second section displaying your custom texts
when it scrolls down and come back it text will appear in first section because when it reaches
if( indexPath.section == 0 )
{
}
it wont do anything so
make it
if( indexPath.section == 0 )
{
cell.textLabel.text = #"";
}
else if( indexPath.section == 2 )
{
cell.textLabel.text = #"";
}
or
if( indexPath.section == 0 )
{
cell.textLabel.text = nil;
}
else if( indexPath.section == 2 )
{
cell.textLabel.text = nil;
}
other FOR SECTION 1 is correct

Related

overriding cells in UITableView while scrolling

I know this is repeated question but i am not getting solution on my issue..I have designed UITableView programmatically but it's working fine till 4 sections or 9 rows but when I am trying to add any extra cell it's overriding first cell on last cell and vice versa.Please solve my issue.Thanks in advance.
my code is -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 6;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
return 1;
else if (section == 1)
return 1;
else if (section == 2)
return 3;
else if (section == 3)
return 2;
else if (section == 4)
return 2;
else if (section == 5)
return 3;
else
return 0;
}
-(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];
}
if ((indexPath.section == 0) && (indexPath.row ==0)) {
cell.textLabel.text = #"";
//Get it from previous view
chooseEventLbl.text = #"Choose an event";
//chooseEventLbl.textColor = [UIColor darkGrayColor];
[cell addSubview:chooseEventLbl];
//cell.textLabel.text = #"Choose an event";
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)];
[image setImage:[UIImage imageNamed:#"arrow_list.png"]];
cell.accessoryView = [[UIImageView alloc]initWithFrame:CGRectMake(260.0, 7.0, 10, 20)];
[cell.accessoryView addSubview:image];
}
else if ((indexPath.section == 1) && (indexPath.row == 0))
{
cell.textLabel.text = #"";
[cell addSubview:squareNameTxt];
}
else if ((indexPath.section == 2) && (indexPath.row == 0))
{
cell.textLabel.text = #"Public";
}
else if ((indexPath.section == 2) && (indexPath.row == 1))
{
cell.textLabel.text = #"Private";
}
else if ((indexPath.row == 2) && (indexPath.row == 2) && (passwordFlag == TRUE))
{
cell.textLabel.text = #"";
[cell addSubview:passwordTxt];
// passwordTxt.enabled = NO;
}
else if ((indexPath.section == 3) && (indexPath.row == 0))
{
cell.textLabel.text = #"Allow multiple Squares";
[aSwitch addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
aSwitch.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:aSwitch];
}
else if ((indexPath.section == 3) && (indexPath.row == 1)) {
cell.textLabel.text = #"";
[cell addSubview:multiSquareListBtn];
}
else if ((indexPath.section == 4) && (indexPath.row == 0))
{
cell.textLabel.text = #"Prizes";
}
else if ((indexPath.section == 4) && (indexPath.row == 1))
{
cell.textLabel.text = #"";
[cell addSubview: firstQuarterTxt];
[cell addSubview:halftimeTxt];
[cell addSubview:thirdQuarterTxt];
[cell addSubview:finalTxt];
}
else if ((indexPath.section == 5) && (indexPath.row ==0))
{
cell.textLabel.text= #"";
currentLocationLbl.text = #"Choose Current Location";
[cell addSubview:currentLocationLbl];
}
else if ((indexPath.section == 5) && (indexPath.row == 1))
{
cell.textLabel.text = #"";
mapLocationLbl.text = #"Choose location from Map";
[cell addSubview:mapLocationLbl];
}
else if ((indexPath.section == 5) && (indexPath.row == 2))
{
cell.textLabel.text = #"";
radiusLocationLbl.text = #"Location Radius";
[cell addSubview:radiusLocationLbl];
}
return cell;
}

Displaying two sections of specific size in a table view

I am trying to fill a table view with an array, but I want two sections, and in the first section I want to display the imformation from indexpath.row == 0 to indexpath.row == 4 and in the second section the information from indexpath.row == 5 to indexpath.row == 9. How can I do that? I have this code for the second section:
if (indexPath.section == 1){
static NSString *CellIdentifier = #"LazyTableCell";
static NSString *PlaceholderCellIdentifier = #"PlaceholderCell";
int nodeCount = [self.entries count];
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
if (nodeCount > 0)
{
if (indexPath.row > 4) {
AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];
cell.textLabel.text = appRecord.artist;
cell.detailTextLabel.text = appRecord.appName;
if (!appRecord.appIcon)
{
if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
{
[self startIconDownload:appRecord forIndexPath:indexPath];
}
cell.imageView.image = [UIImage imageNamed:#"Placeholder.png"];
}
else
{
UIImage *image = appRecord.appIcon;
CGSize itemSize = CGSizeMake(83.0, 48.0);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, 83.0, 48.0);
[image drawInRect:imageRect];
appRecord.appIcon = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = appRecord.appIcon;
}
}
return cell;
}
}
set the number of section in this function
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
Perhaps you also want to implement
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return #"section title";
}
set the number of lines in the following function
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5; // since you have 0-4 and 5-9 for the two sections
}
after seeing in your code [self.entries objectAtIndex:indexPath.row];
I assumed self.entries is the one array that you have been talking about.
You can initialize the UITableViewCell property in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
by using a combination of section and row to get the index of your self.entries, probably something like
AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row+5*indexPath.section];
I hope i got your question right.

how to customize the tableview has shown in figure

How to create the tableview has shown below picture
.
If i created section i am missing th boarder line what to do ?
You need to set the UITableView to Grouped Style. Then you need 2 Sections, with no Header.
also you have to set the
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
Example Code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if(section == 0)
return 1;
else
return 2;
}
- (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];
}
// Configure the cell...
switch(indexPath.section)
{
case 0:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"Create a Holiday Card";
break;
case 1:
switch(indexPath.row)
{
case 0:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"Login to Photo";
break;
case 1:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"Create a Free Account";
break;
}
}
return cell;
}
Per example above
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;// Amount of sections
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0){
return 1;
}
else if (section == 1){
return 2;
}
}
- (void)tableViewUITableView *)aTableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
I assume you are referring to the STYLE of that example.
[self.view setBackgroundColor: [UIColor blueColor]];
/* also assuming your table sits on a UIViewController */
[myTableView setBackgroundColor: [UIColor clearColor]];
[myTableView setOpaque: NO];

Each data opening their own nib files in Table View

Hey guys,
I have a Table View sorted out properly, but I have a problem, each data on my Table View is opening the one and same nib file when I'm using this code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 4;
}
// Category
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {
if (section == 0) return #"In-Site SEO";
if (section == 1) return #"Inside Analysis";
if (section == 2) return #"Ranks N Stuff";
if (section == 3) return #"Server Info";
return #"Other";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
if (section == 0) return 7;
if (section == 1) return 3;
if (section == 2) return 6;
if (section == 3) return 5;
return 0;
}
// Customize the appearance of table view cells.
- (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];
}
// Configure the cell...
NSUInteger row = [indexPath row];
if ( indexPath.section == 1 ) row += 7;
if ( indexPath.section == 2 ) row += 10;
if ( indexPath.section == 3 ) row += 16;
if ( indexPath.section == 4 ) row += 21;
cell.textLabel.text = [glossaryArray objectAtIndex:row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger row = [indexPath row];
if (self.glossaryDetailViewController == nil) {
GlossaryDetailViewController *aGlossaryDetail = [[GlossaryDetailViewController alloc] initWithNibName:#"GlossaryDetailViewController" bundle:nil];
self.glossaryDetailViewController = aGlossaryDetail;
[aGlossaryDetail release];
}
glossaryDetailViewController.title = [NSString stringWithFormat:#"%#", [glossaryArray objectAtIndex:row]];
NewReferencemoi_caAppDelegate *delegate = (NewReferencemoi_caAppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.glossaryNavController pushViewController:glossaryDetailViewController animated:YES];
}
GlossaryDetailViewController is my implementation file and also I added the nib file for that,
So hopefully someone can help me how to have each data open their own view, I won't really mind if I have to make a nib file for each and all my data on my Table View,
thanks
To put in your code style you can try
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger row = [indexPath row];
NSString *nibName;
if ( indexPath.section == 1 ) nibName = #"firstNib";
if ( indexPath.section == 2 ) nibName = #"secondNib";
if ( indexPath.section == 3 ) nibName = #"thirdNib";
if ( indexPath.section == 4 ) nibName = #"fourthNib";
GlossaryDetailViewController *aGlossaryDetail = [[GlossaryDetailViewController alloc] initWithNibName:nibName bundle:nil];
self.glossaryDetailViewController = aGlossaryDetail;
[aGlossaryDetail release];
glossaryDetailViewController.title = [NSString stringWithFormat:#"%#", [glossaryArray objectAtIndex:row]];
NewReferencemoi_caAppDelegate *delegate = (NewReferencemoi_caAppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.glossaryNavController pushViewController:glossaryDetailViewController animated:YES];
}

Cells reorganizing themselves on iPhone?

I'm writing an app for my father's business, it involves lists, however, I have seen that it seems to reorganize the list, even though I don't want it to. For example, all of section 1 is supposed to say one thing, when I scroll down, content from the other sections is put in section 1's cells, this goes for the other sections as well.
Here's all the table code, if it helps.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == ENGINEER_ID) {
return 3;
} else if (section == FIREMAN_ID) {
return 3;
} else if (section == CONDUCTOR_ID) {
return 3;
} else if (section == TRAINMASTER_ID) {
return 3;
} else {
return 0;
}
}
// Customize the appearance of table view cells.
- (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];
if ([indexPath section] == ENGINEER_ID) {
cell.textLabel.text = #"Engineer";
} else if ([indexPath section] == FIREMAN_ID) {
cell.textLabel.text = #"Fireman";
} else if ([indexPath section] == CONDUCTOR_ID) {
cell.textLabel.text = #"Conductor";
} else if ([indexPath section] == TRAINMASTER_ID) {
cell.textLabel.text = #"Trainmaster";
}
}
// Configure the cell.
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == ENGINEER_ID) {
return #"Master Engineer II";
} else if (section == FIREMAN_ID) {
return #"Fireman";
} else if (section == CONDUCTOR_ID) {
return #"Conductor";
} else if (section == TRAINMASTER_ID) {
return #"Trainmaster";
} else {
return #"What.";
}
}
You need to move the assignment of text out of the conditional.
After the first 3 cells's are alloc/init'd, you won't necessarily be getting more cells.
You are assigning cell.textLabel.text only if you create a new cell.
Logic should be:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if ([indexPath section] == ENGINEER_ID) {
cell.textLabel.text = #"Engineer";
} else if ([indexPath section] == FIREMAN_ID) {
cell.textLabel.text = #"Fireman";
} else if ([indexPath section] == CONDUCTOR_ID) {
cell.textLabel.text = #"Conductor";
} else if ([indexPath section] == TRAINMASTER_ID) {
cell.textLabel.text = #"Trainmaster";
}
And of course after you tire of writing cascading if statements, you could use switch([indexPath section])