overriding cells in UITableView while scrolling - objective-c

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;
}

Related

UITableView values don't match its key form plist

I am making a plist viewer and editor for iOS, but the problem I have it, that the values don't match to its key. Like I have this plist
{
BuildMachineOSBuild = 12C54;
CAProcessCanAccessGPU = 1;
CFBundleAllowMixedLocalizations = 1;
CFBundleDevelopmentRegion = English;
CFBundleDisplayName = iAd;
CFBundleExecutable = AdSheet;
CFBundleIconFiles = (
"iAd.png",
"iAd#2x.png"
);
CFBundleIcons = {
CFBundlePrimaryIcon = {
CFBundleIconFiles = (
"iAd.png",
"iAd#2x.png"
);
UIPrerenderedIcon = 0;
};
};
CFBundleIdentifier = "com.apple.AdSheetPhone";
CFBundleInfoDictionaryVersion = "6.0";
CFBundleName = AdSheet;
CFBundlePackageType = APPL;
CFBundleResourceSpecification = "ResourceRules.plist";
CFBundleShortVersionString = "1.0";
CFBundleSignature = "????";
CFBundleSupportedPlatforms = (
iPhoneOS
);
CFBundleVersion = "1.0";
CLSystemService = 1;
CLVisibleImmediately = 1;
CanInheritApplicationStateFromOtherProcesses = 1;
DTCompiler = "com.apple.compilers.llvm.clang.1_0";
DTPlatformBuild = "";
DTPlatformName = iphoneos;
DTPlatformVersion = "7.0";
DTSDKBuild = 11A450;
DTSDKName = "iphoneos7.0.internal";
DTXcode = 0500;
DTXcodeBuild = 5A1344i;
MinimumOSVersion = "7.0";
NSPrincipalClass = ADSApplication;
SBAppTags = (
hidden
);
SBMachServices = (
"com.apple.AdSheetPhone.server",
"com.apple.AdSheetPhone.management",
"com.apple.uikit.viewservice.com.apple.AdSheetPhone"
);
UIBackgroundModes = (
continuous
);
UIDeviceFamily = (
1,
2
);
UIShouldIgnoreRemoteControlEvents = 1;
UIStatusBarHidden = 1;
UISupportedInterfaceOrientations = (
UIInterfaceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft,
UIInterfaceOrientationLandscapeRight
);
UIViewServiceUsesNSXPCConnection = 1;
}
)
I try to display the value and its key (or Dictionary for Dictionaries, Array for Arrays and Bool for Bools)
Then for example CanInheritApplicationStateFromOtherProcesses should show a 1 (wich is a BOOL), but is don't show any value. And CanInheritApplicationStateFromOtherProcesses is on index 0 of my UITableView, but it is on index 19 of the plist. Does anyone know how to fix this error. I am displaying the key in the cell.textLabel.text and the value in cell.detailTextLabel.text. Here is my code of - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"plist Cell" forIndexPath:indexPath];
id obj;
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.detailTextLabel.minimumScaleFactor = 0.5;
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
cell.editingAccessoryType = UITableViewCellEditingStyleNone;
if (self.plistDict) {
NSString* key = [[self.plistDict allKeys] objectAtIndex:indexPath.row];
cell.textLabel.text = key;
obj = [self.plistDict objectForKey:key];
} else {
cell.textLabel.text = [NSString stringWithFormat:#"%li", (long)indexPath.row];
obj = [self.plistArray objectAtIndex:indexPath.row];
}
if(([obj isKindOfClass:[NSArray class]] || [obj isKindOfClass:[NSMutableArray class]]) && cell.tag == 0){
cell.detailTextLabel.text = NSLocalizedString(#"Array", nil);
cell.tag = 1;
return cell;
} if(([obj isKindOfClass:[NSDictionary class]] || [obj isKindOfClass:[NSMutableDictionary class]]) && cell.tag == 0){
cell.detailTextLabel.text = NSLocalizedString(#"Dictionary", nil);
cell.tag = 2;
return cell;
} if ([obj isKindOfClass:[NSString class]] && cell.tag == 0) {
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#", (NSString *)obj];
cell.tag = 3;
return cell;
} if ([obj isKindOfClass:[NSNumber class]]) {
if ([obj isKindOfClass:[#(YES) class]] && cell.tag == 0) {
[boolSwitch setOn:(BOOL)obj animated:YES];
[cell.contentView addSubview:boolSwitch];
cell.detailTextLabel.textColor = [UIColor clearColor];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.tag = 7;
return cell;
} else if (![obj isKindOfClass:[#(YES) class]] && cell.tag == 0) {
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#", (NSNumber *)obj];
cell.tag = 4;
return cell;
} else {
return cell;
}
} if ([obj isKindOfClass:[NSData class]] && cell.tag == 0) {
cell.detailTextLabel.text = NSLocalizedString(#"Data", nil);
cell.tag = 5;
return cell;
} if ([obj isKindOfClass:[NSDate class]] && cell.tag == 0) {
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#", (NSDate *)obj];
cell.tag = 6;
return cell;
} else {
return cell;
}
The odd thing is for the most keys the value matches, but not for all.
cell will be reused with tag. so you maybe should reset the tag before reuse the cell:
//CustomeCell.m
- (void)prepareForReuse{
[super prepareForReuse];
self.tag = 0;
}
Or in tableView delegate class:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.tag = 0;
}

UITableView cell labels moving around when scrolling

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

Detailtext is only showing in first tab

Got the code but detailText is only showing in the first 'column' in case 0
code is under: cellForRowAtIndexPath
if (indexPath.row == 0) {
cell.detailTextLabel.text = #"A";
}
}
else if (indexPath.row == 1){
cell.detailTextLabel.text = #"B";
}
else if (indexPath.row == 2){
cell.detailTextLabel.text = #"C";
}
else if (indexPath.row == 3){
cell.detailTextLabel.text = #"D";
}
There is an obvious right-brace mismatch, change your code to:
if (indexPath.row == 0) {
cell.detailTextLabel.text = #"A";
}
else if (indexPath.row == 1) {
cell.detailTextLabel.text = #"B";
...
You can shorten that to
cell.detailTextLabel.text = [#"ABCD" substringWithRange:NSMakeRange(indexPath.row,1)];

UITableViewCell issue indexPath.row not displaying correctly

I have set up a UITableView which has 3 sections in, with 3 rows in each. I ahve used the following code to specify that I want the objects from certain arrays to appear as the row content;
recipeData = [[NSArray alloc] initWithObjects:#"Chicken & Veg Rissoto",#"Super Healthy Pizza",#"Salmon Burgers",nil];
dessertsData = [[NSArray alloc] initWithObjects:#"Raspberry Parfaits",#"ChocNana YumYum",#"Grilled Choc Sandwich",nil];
beveragesData = [[NSArray alloc] initWithObjects:#"Berry Smoothie",#"Banana Berry Smoothie",#"Cocoa Soy Smoothie",nil];
[self setTitle:#"Recipe Table"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.recipeData count];
return [self.dessertsData count];
return [self.beveragesData count];
This was all working fine, until this evening when I added in images to the left of the text in the row. The images display perfectly, but for some reason the rows are all populated with the objects from beveragesData and it totally disregards the other two sets of objects. The code is below that I have used. I am hoping it is something as simple as just writing the if statements incorrectly.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SimpleTableIdentifier] autorelease];
}
if(indexPath.section == 0)
if(indexPath.row == 0)
[cell.imageView setImage:[UIImage imageNamed:#"Main0.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
if(indexPath.section == 0)
if(indexPath.row == 1)
[cell.imageView setImage:[UIImage imageNamed:#"Main1.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
if(indexPath.section == 0)
if(indexPath.row == 2)
[cell.imageView setImage:[UIImage imageNamed:#"Main2.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
if(indexPath.section == 1)
if(indexPath.row == 0)
[cell.imageView setImage:[UIImage imageNamed:#"Dessert0.png"]];
[cell.textLabel setText:[dessertsData objectAtIndex:indexPath.row]];
if(indexPath.section == 1)
if(indexPath.row == 1)
[cell.imageView setImage:[UIImage imageNamed:#"Dessert1.png"]];
[cell.textLabel setText:[dessertsData objectAtIndex:indexPath.row]];
if(indexPath.section == 1)
if(indexPath.row == 2)
[cell.imageView setImage:[UIImage imageNamed:#"Dessert2.png"]];
[cell.textLabel setText:[dessertsData objectAtIndex:indexPath.row]];
if (indexPath.section == 2)
if(indexPath.row == 0)
[cell.imageView setImage:[UIImage imageNamed:#"Drink0.png"]];
[cell.textLabel setText:[beveragesData objectAtIndex:indexPath.row]];
if(indexPath.section == 2)
if(indexPath.row == 1)
[cell.imageView setImage:[UIImage imageNamed:#"Drink1.png"]];
[cell.textLabel setText:[beveragesData objectAtIndex:indexPath.row]];
if(indexPath.section == 2)
if(indexPath.row == 2)
[cell.imageView setImage:[UIImage imageNamed:#"Drink2.png"]];
[cell.textLabel setText:[beveragesData objectAtIndex:indexPath.row]];
return cell;
So at present, when that runs all 9 rows of the UITableView are populated with the objects from beveragesData. Any help would be greatly appreciated.
Thanks
Objective-c structure isn't based on indentation
This:
if(indexPath.section == 0)
if(indexPath.row == 0)
[cell.imageView setImage:[UIImage imageNamed:#"Main0.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
Should be:
if(indexPath.section == 0)
if(indexPath.row == 0) {
[cell.imageView setImage:[UIImage imageNamed:#"Main0.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
}
It's like C/C++/Java -- the if applies to the next statement. If you need more than one, you need to enclose the statements in curly braces. You could also put in curlies all of the time to not worry about it.
Also, this code
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.recipeData count];
return [self.dessertsData count];
return [self.beveragesData count];
}
Doesn't do what you think it does. The first return is always run and the second two lines are ignored (should have gotten a warning about that).
Your numberOfRowsInSection: section is not correct either; should be:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ( section == 0 )
return [self.recipeData count];
if ( section == 1 )
return [self.dessertsData count];
if ( section == 3 )
return [self.beveragesData count];
}
You should also use a switch statement:
switch ( indexPath.section )
{
case 0:
{
if(indexPath.row == 0) {
[cell.imageView setImage:[UIImage imageNamed:#"Main0.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
}
if(indexPath.row == 1) {
[cell.imageView setImage:[UIImage imageNamed:#"Main1.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
}
if(indexPath.row == 2) {
[cell.imageView setImage:[UIImage imageNamed:#"Main2.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
}
}
}
/* etc. */

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])