Detailtext is only showing in first tab - objective-c

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

Related

How can I optimize if-else statement to be more simple?

Im working with the code below in developing simple ios app using objective-C.Is that have another way to optimize this code:
if ([segue.identifier isEqualToString:#"showCommitDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
CommitDetailsTableViewController *destViewController = segue.destinationViewController;
RepoObject *repoObj = [self.RepoListArray objectAtIndex:indexPath.row];
NSString *repoCommit_url = [repoObj.Commit_url stringByReplacingOccurrencesOfString:#"{/sha}" withString:#""];
if (indexPath.row == 0) {
NSString *SpringBootURL = repoCommit_url;
self.commit_url = SpringBootURL;
destViewController.CommitRepoURL = self.commit_url;
destViewController.navigationItem.title = #"Spring-Integration-in-Action";
}else if (indexPath.row == 1){
NSString *SpringFrameworkURL = repoCommit_url;
self.commit_url = SpringFrameworkURL;
destViewController.CommitRepoURL = self.commit_url;
destViewController.navigationItem.title = #"spring-data-jdbc-ext";
}else if (indexPath.row == 2){
NSString *SpringAmqpURL = repoCommit_url;
self.commit_url = SpringAmqpURL;
destViewController.CommitRepoURL = self.commit_url;
destViewController.navigationItem.title = #"spring-data-commons";
}else if (indexPath.row == 3){
NSString *SpringIdeURL = repoCommit_url;
self.commit_url = SpringIdeURL;
destViewController.CommitRepoURL = self.commit_url;
destViewController.navigationItem.title = #"spring-data-graph";
}else{
NSString *SpringIntegratURL = repoCommit_url;
self.commit_url = SpringIntegratURL;
destViewController.CommitRepoURL = self.commit_url;
destViewController.navigationItem.title = #"spring-data-document-examples";
}
}
Beside that, how can i assign title directly without hard-coding it like i did above.?
if ([segue.identifier isEqualToString:#"showCommitDetail"]) {
NSInteger selectedRow = [self.tableView indexPathForSelectedRow].row;
CommitDetailsTableViewController *destViewController = segue.destinationViewController;
RepoObject *repoObj = [self.RepoListArray objectAtIndex:selectedRow];
self.commit_url = [repoObj.Commit_url stringByReplacingOccurrencesOfString:#"{/sha}" withString:#""];
destViewController.CommitRepoURL = self.commit_url;
NSString *title;
switch (selectedRow) {
case 0:
title = #"Spring-Integration-in-Action";
break;
case 1:
title = #"spring-data-jdbc-ext";
break;
case 2:
title = #"spring-data-commons";
break;
case 3:
title = #"spring-data-graph";
break;
default:
title = #"spring-data-document-examples";
break;
}
destViewController.navigationItem.title = title;
}
You can try change the second if elses to the following, it will optimize lines of code and legibility.
NSString *url = repoCommit_url;
self.commit_url = url;
destViewController.CommitRepoURL = self.commit_url;
if (indexPath.row == 0) {
destViewController.navigationItem.title = #"Spring-Integration-in-Action";
}else if (indexPath.row == 1){
destViewController.navigationItem.title = #"spring-data-jdbc-ext";
}else if (indexPath.row == 2){
destViewController.navigationItem.title = #"spring-data-commons";
}else if (indexPath.row == 3){
destViewController.navigationItem.title = #"spring-data-graph";
}else{
destViewController.navigationItem.title = #"spring-data-document-examples";
}

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

delete a character from a string Xcode

I am using the following code to add a character to a mutable string then put it into a textview from a custom keyboard
- (IBAction) press:(id)sender {
[[UIDevice currentDevice] playInputClick];
if (sender == AEE) {
self.BACK.hidden = NO;
A = #"A";
NSLog(#"sender:%#",sender);
}
if (sender == BEE) {
self.BACK.hidden = NO;
A = #"B";
}
if (sender == CEE) {
self.BACK.hidden = NO;
A = #"C";
}
if (sender == DEE) {
self.BACK.hidden = NO;
A = #"D";
}
if (sender == EEE) {
self.BACK.hidden = NO;
A = #"E";
}
if (sender == EFF) {
self.BACK.hidden = NO;
A = #"F";
}
if (sender == ONE) {
self.BACK.hidden = NO;
A = #"1";
}
if (sender == TWO) {
self.BACK.hidden = NO;
A = #"2";
}
if (sender == THREE) {
self.BACK.hidden = NO;
A = #"3";
}
if (sender == FOUR) {
self.BACK.hidden = NO;
A = #"4";
}
if (sender == FIVE) {
self.BACK.hidden = NO;
A = #"5";
}
if (sender == SIX) {
self.BACK.hidden = NO;
A = #"6";
}
if (sender == SEVEN) {
self.BACK.hidden = NO;
A = #"7";
}
if (sender == EIGHT) {
self.BACK.hidden = NO;
A = #"8";
}
if (sender == NINE) {
self.BACK.hidden = NO;
A = #"9";
}
if (sender == ZERO) {
self.BACK.hidden = NO;
A = #"0";
}
if (display.length <= 7) {
[self.display appendString:A];
DONE.hidden = YES;
}
if (display.length == 8) {
DONE.hidden = NO;
}
else {
[self.display appendString:#""];
}
//**THIS IS WHERE I DELETE CHARATERS**
if (sender == BACK) {
[display deleteCharactersInRange:NSMakeRange([display length]-2, 2)];
NSLog(#"display2:%#", display);
if (self.display.length <1) {
BACK.hidden = YES;
}
if (display.length < 8) {
DONE.hidden = YES;
}
}
you.text = display;
}
When I use the code to delete characters it works fine with for all the characters 1 - 7. Wherever I am if I hit the backspace button it deletes one character. However when I hit the backspace button to delete a character from having 8 characters it deletes two characters. If I change the code to only delete one character like
[display deleteCharactersInRange:NSMakeRange([display length]-1, 1)];
it only deletes one character once. And if I put
[display deleteCharactersInRange:NSMakeRange([display length]-1, 2)];
it does the same thing. But when I put
[display deleteCharactersInRange:NSMakeRange([display length]-2, 1)];
my app crashes and I get an out of bounds error. I have tried using if display =<7 use one the -2,2 code and if display ==8 use the -1,1 code but nothing make it reduce the number of character by one all the time. Does anyone have any suggestions?
I figured out that if I created a separate action to delete and used the following code everything seemed to work ok
(IBAction) Delete:(id)sender{
[[UIDevice currentDevice] playInputClick];
if (sender == BACK) {
if ([display length] > 0) {
[display setString:[display substringToIndex:[display length]-1]];
you.text = display;
}
else {
you.text = display;
//no characters to delete... attempting to do so will result in a crash
}
if (self.display.length <1) {
BACK.hidden = YES;
}
if (display.length < 8) {
DONE.hidden = YES;
}
}

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