gpt4 book ai didi

ios - 以编程方式将标签/字符串添加到 UITableView,就像在 iPhone 设置中看到的那样

转载 作者:行者123 更新时间:2023-11-28 20:29:28 25 4
gpt4 key购买 nike

我有一个包含多个表格的应用程序,就像在 iPhone 的“设置”中看到的那样。我想添加一个要在此标签中显示的值,就像在设置 Wi-Fi 或蓝牙中看到的一样,在右侧您有您的项目,在左侧显示一个值。我该怎么做?

设置.h

@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

设置.m

@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

接下来,在我的 connectionDidFinishLoading 中,我从对我的 WebAPI 的 JSON 调用中获取了结果,并填充了这个设置对象....

-(void) connectionDidFinishLoading:(NSURLConnection *)connection {

self.settings = [NSMutableArray array];

NSError *error = nil;

id result = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&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;
}

When the page first loads...

After I pull down on the UI to get the cellFroRowAtIndexPath function to fire again...

我在 AppDelegate.h 中综合了我的 selected_value 并使用它来存储用户点击的单元格的值。

@interface AppDelegate : UIResponder <UIApplicationDelegate> {    
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

AppDelegate.m

这是我的 cellForRowAtIndexPath

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

在我的日志窗口中,除非我向下滚动,否则我会看到“选择值:是 6”。呈现给表的最后一个值是我最终看到的。我知道有更好的方法可以做到这一点,我只是不知道怎么做。想法/建议?

最佳答案

您需要使用 UITableViewCellStyleValue1 样式设置表格 View 单元格。这是“设置”应用中用于显示黑色标题和蓝色值的类型。

使用 titleLabel 为单元格设置标题,使用单元格的 detailTextLabel 属性设置右侧的值。

在文档中查找 UITableViewCell 和 initWithStyle:reuseIdentifier: 方法。

关于ios - 以编程方式将标签/字符串添加到 UITableView,就像在 iPhone 设置中看到的那样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12827975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com