Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd label/string to UITableView programmatically like seen in iPhone Settings
    text
    copied!<p>I have an application with several tables separated exactly like seen in the Settings on the iPhone. I would like to add a value to be displayed in this label exactly like seen for example in the Settings Wi-Fi or Bluetooth where on the right you have your item and on the left a value is displayed. How can I do this?</p> <p>Setting.h</p> <pre><code>@interface Setting : NSObject { NSString *_settingsID; NSString *_catalogID; NSString *_category; NSString *_facings; NSString *_quantity; } @property (nonatomic, retain) NSString *settingsID; @property (nonatomic, retain) NSString *catalogID; @property (nonatomic, retain) NSString *category; @property (nonatomic, retain) NSString *facings; @property (nonatomic, retain) NSString *quanity; - (Setting *)initWithName:(NSString *)settingsID desc:(NSString *)category CategoryID:(NSString *)catalogID Facings:(NSString *)facings Quantity:(NSString *)quantity; @end </code></pre> <p>Setting.m</p> <pre><code>@implementation Setting @synthesize settingsID = _settingsID; @synthesize catalogID = _catalogID; @synthesize category = _category; @synthesize facings = _facings; @synthesize quanity = _quantity; - (Setting *)initWithName:(NSString *)settingsID desc:(NSString *)category CategoryID:(NSString *)catalogID Facings:(NSString *)facings Quantity:(NSString *)quantity { if ((self = [super init])) { self.settingsID = settingsID; self.catalogID = catalogID; self.category = category; self.facings = facings; self.quanity = quantity; } return self; } @end </code></pre> <p>Next in my connectionDidFinishLoading I have taken the result from the JSON call to my WebAPI and I populate this Setting object....</p> <pre><code>-(void) connectionDidFinishLoading:(NSURLConnection *)connection { self.settings = [NSMutableArray array]; NSError *error = nil; id result = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&amp;error]; if ([result isKindOfClass:[NSArray class]]) { for (NSDictionary *item in result) { NSString *settingsID = [item objectForKey:@"ID"]; NSString *category = [item objectForKey:@"Category"]; NSString *categoryID = [item objectForKey:@"CatalogID"]; NSString *facings = [item objectForKey:@"Facings"]; NSString *quantity = [item objectForKey:@"Quantity"]; Setting *setting = [[Setting alloc] initWithName:settingsID desc:category CategoryID:categoryID Facings:facings Quantity:quantity]; [self.settings addObject:setting]; } } for (Setting *item in self.settings) { NSString *frozen = @"Frozen"; BOOL res = [frozen isEqualToString:item.category]; if (res == TRUE) { **NSLog(@"Original Value: %@, Bool Value: %@", item.facings, item.facings.boolValue == false ? @"No" : @"Yes"); NSLog(@"Original Value: %@, Bool Value: %@", item.quantity, item.quantity.boolValue == false ? @"No" : @"Yes"); // Frozen appDelegate.frozen_facing_value = item.facings.boolValue == false ? @"No" : @"Yes"; appDelegate.frozen_quantity_value = item.quantity.boolValue == false ? @"No" : @"Yes";** } NSString *fruit = @"Fruit"; res = [fruit isEqualToString:item.category]; if (res == TRUE) { // Fruit } NSString *salads = @"Salads"; res = [salads isEqualToString:item.category]; if (res == TRUE) { // Salads } NSString *vegetables = @"Vegetables"; res = [vegetables isEqualToString:item.category]; if (res == TRUE) { // Vegetables } } NSLog(@"Finished.."); finished = TRUE; } </code></pre> <p><img src="https://i.stack.imgur.com/IzT0D.png" alt="When the page first loads..."></p> <p><img src="https://i.stack.imgur.com/sWvbQ.png" alt="After I pull down on the UI to get the cellFroRowAtIndexPath function to fire again..."></p> <p>I synthesize my selected_value in AppDelegate.h and use this to store the value of the cell the user clicks on.</p> <pre><code>@interface AppDelegate : UIResponder &lt;UIApplicationDelegate&gt; { NSString *_selected_value; **NSString *_frozen_facing_value;** } @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) UINavigationController *navigationController; @property (nonatomic, retain) NSString *selected_value; **@property (nonatomic, retain) NSString *frozen_facing_value;** @end </code></pre> <p>AppDelegate.m</p> <p>Here is my cellForRowAtIndexPath</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *result = nil; if ([tableView isEqual:self.myTableView]){ static NSString *CellIdentifier = @"CellIdentifier"; result = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (result == nil){ result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; } NSMutableArray *sectionArray = [self.arrayOfSections objectAtIndex:indexPath.section]; result.textLabel.text = [sectionArray objectAtIndex:indexPath.row]; NSString *res = (NSString *)result.textLabel.text; AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; if ([[self tableView:tableView titleForHeaderInSection:indexPath.section] isEqualToString:@"Frozen"]) { if ([res isEqualToString:FACINGS]) { **result.detailTextLabel.text = appDelegate.frozen_facing_value;** } else if ([res isEqualToString:QUANTITY]) { result.detailTextLabel.text = @"Yes 2"; } } else if ([[self tableView:tableView titleForHeaderInSection:indexPath.section] isEqualToString:@"Fruit"]) { if ([res isEqualToString:FACINGS]) { result.detailTextLabel.text = @"No 3"; } else if ([res isEqualToString:QUANTITY]) { result.detailTextLabel.text = @"Yes 4"; } } else if ([[self tableView:tableView titleForHeaderInSection:indexPath.section] isEqualToString:@"Salads"]) { if ([res isEqualToString:FACINGS]) { result.detailTextLabel.text = @"No 5"; } else if ([res isEqualToString:QUANTITY]) { result.detailTextLabel.text = @"Yes 6"; } } else if ([[self tableView:tableView titleForHeaderInSection:indexPath.section] isEqualToString:@"Vegetables"]) { if ([res isEqualToString:FACINGS]) { result.detailTextLabel.text = @"No 7"; } else if ([res isEqualToString:QUANTITY]) { result.detailTextLabel.text = @"Yes 8"; } } appDelegate.selected_value = result.detailTextLabel.text; result.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } return result; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; NSMutableArray *sectionArray = [self.arrayOfSections objectAtIndex:indexPath.section]; NSLog(@"Select value: %@", appDelegate.selected_value); appDelegate.selectedSetting = [[NSString alloc] initWithFormat:@"Select value: %@ for %@, in section: %@", @"Test", [sectionArray objectAtIndex:indexPath.row], [self tableView:tableView titleForHeaderInSection:indexPath.section]]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; SettingsViewController *settings = [[SettingsViewController alloc] initWithNibName:@"Settings" bundle:[NSBundle mainBundle]]; [self.navigationController pushViewController:settings animated:YES]; } </code></pre> <p>In my log window I see "Select value: Yes 6" unless I scroll down. The last value rendered to the table is what I end up seeing. I know there is a better way to do this, I just don't know how. Ideas/Suggestions?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload