• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何在自定义 UITableViewCell 中的 Dictionary 中显示数据?

[复制链接]
菜鸟教程小白 发表于 2022-12-13 14:29:53 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在使用 AFNetworking 3.0

在 View Controller 中,我在每个单元格中显示了多个专业标题。点击特定单元格后,我得到了服务器的响应,如下所示:

[{"dp":{"id":0,"qualification":"MD(Doctor)","reg_id":0,"specialization1":"Orthopaedics","specialization2":"Dermatologist","specialization3":"Neurology","url":"www.batras.com"},"ds":{"city":"agiripalle","consultation_fee":450,"contact_no":"1234567890","country":"India","id":18,"organization_name":"Batra","pin_code":"411052","reg_id":0,"state":"andhra pradesh","street_name":"jhbfahjf","wellness_flag":false},"reg":{"mobile_no":0,"name":"Batra","profile_id":0,"reg_id":157,"wellness_id":"251215782521"}},{"dp":{"id":0,"qualification":"MD(Doctor)","reg_id":0,"specialization1":"Orthopaedics","specialization2":"Dermatologist","specialization3":"Neurology","url":"www.batras.com"},"ds":{"city":"anur","consultation_fee":458,"contact_no":"1234567898","country":"India","id":19,"organization_name":"Danny","pin_code":"411052","reg_id":0,"state":"himachal pradesh","street_name":"afafwfw","wellness_flag":false},"reg":{"mobile_no":0,"name":"Batra","profile_id":0,"reg_id":157,"wellness_id":"251215782521"}},{"dp":{"id":0,"qualification":"MD(Doctor)","reg_id":0,"specialization1":"Orthopaedics","specialization2":"Dermatologist","specialization3":"Neurology","url":"www.batras.com"},"ds":{"city":"pabal","consultation_fee":500,"contact_no":"4512451252","country":"India","id":20,"organization_name":"Org Name","pin_code":"411052","reg_id":0,"state":"maharashtra","street_name":"afawafww","wellness_flag":false},"reg":{"mobile_no":0,"name":"Batra","profile_id":0,"reg_id":157,"wellness_id":"251215782521"}},{"dp":{"id":0,"qualification":"MD(Doctor)","reg_id":0,"specialization1":"Orthopaedics","specialization2":"Dermatologist","specialization3":"Neurology","specialization4":"Gynecologist","url":"danny.com"},"ds":{"city":"bumlitan","consultation_fee":504,"contact_no":"1234567898","country":"India","id":21,"organization_name":"Danny","pin_code":"411052","reg_id":0,"state":"andaman and nicobar islands","street_name":"East Street","wellness_flag":false},"reg":{"mobile_no":0,"name":"Danny","profile_id":0,"reg_id":167,"wellness_id":"311220165848"}},{"dp":{"id":0,"qualification":"MD(Doctor)","reg_id":0,"specialization1":"Orthopaedics","specialization2":"Dermatologist","specialization3":"Neurology","specialization4":"Gynecologist","url":"danny.com"},"ds":{"city":"amtala","consultation_fee":451,"contact_no":"1234567898","country":"India","id":22,"organization_name":"Danny","pin_code":"411052","reg_id":0,"state":"assam","street_name":"West","wellness_flag":false},"reg":{"mobile_no":0,"name":"Danny","profile_id":0,"reg_id":167,"wellness_id":"311220165848"}},{"dp":{"id":0,"qualification":"MD(Doctor)","reg_id":0,"specialization1":"Orthopaedics","specialization2":"Dermatologist","specialization3":"Neurology","specialization4":"Gynecologist","url":"danny.com"},"ds":{"city":"hala","consultation_fee":541,"contact_no":"1234567890","country":"India","id":23,"organization_name":"Danny Clinic","pin_code":"411123","reg_id":0,"state":"chandigarh","street_name":"Chandigarh","wellness_flag":false},"reg":{"mobile_no":0,"name":"Danny","profile_id":0,"reg_id":167,"wellness_id":"311220165848"}}]

之后,我按以下方式处理该响应:

NSLog(@"Response from specialization server  :  %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
NSArray *ResponseArray = [NSJSONSerialization JSONObjectWithData: responseObject options: kNilOptions error: nil];
if (ResponseArray.count >0)
{
    _spclarr = [ResponseArray mutableCopy];
    NSLog(@"special_array : %@",_spclarr);
    _spclstr=[NSString stringWithFormat"%@",_spclarr];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    // saving an NSString
    [prefs setObject:_spclstr forKey"spcl"];
    [self performSegueWithIdentifier"doctorspcl" sender:self];
}
[self getdata:responseObject];
} failure:^(NSURLSessionTask *operation, NSError *error)
          {
     // If Error occure, then this is AlertController Appear
                  NSLog(@"Error: %@", error);

在下一个 View 中:

- (void)viewDidLoad {
    [super viewDidLoad];
            NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *s = [prefs stringForKey"spcl"];
    NSLog(@"spcl: %@",s);
    menuItems=[s componentsSeparatedByString","];
    NSLog(@"menu: %@",menuItems);
}
- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{
    return [menuItems count];
}
- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Doctordetail";
            DoctorDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
            NSDictionary *content = [menuItems objectAtIndex:indexPath.row];
    NSLog(@"content: %@",content);
return cell;
}

- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
    NSLog(@"%ld",(long)indexPath.row);
            NSDictionary *content = [menuItems objectAtIndex:indexPath.row];
    NSLog(@"Cell Clicked: %@",menuItems);
    NSLog(@"content2: %@",content);
    }

当我在模拟器上运行应用程序时,它可以工作。当我单击特定单元格时,日志会打印该单元格的数据。

这里是菜单项:

menu: (
    "(\n        {\n        dp =         {\n            id = 0;\n            qualification = \"MD(Doctor)\";\n            \"reg_id\" = 0;\n            specialization1 = Orthopaedics;\n            specialization2 = Dermatologist;\n            specialization3 = Neurology;\n            url = \"www.batras.com\";\n        };\n        ds =         {\n            city = agiripalle;\n            \"consultation_fee\" = 450;\n            \"contact_no\" = 1234567890;\n            country = India;\n            id = 18;\n            \"organization_name\" = Batra;\n            \"pin_code\" = 411052;\n            \"reg_id\" = 0;\n            state = \"andhra pradesh\";\n            \"street_name\" = jhbfahjf;\n            \"wellness_flag\" = 0;\n        };\n        reg =         {\n            \"mobile_no\" = 0;\n            name = Batra;\n            \"profile_id\" = 0;\n            \"reg_id\" = 157;\n            \"wellness_id\" = 251215782521;\n        };\n    }",
    "\n        {\n        dp =         {\n            id = 0;\n            qualification = \"MD(Doctor)\";\n            \"reg_id\" = 0;\n            specialization1 = Orthopaedics;\n            specialization2 = Dermatologist;\n            specialization3 = Neurology;\n            url = \"www.batras.com\";\n        };\n        ds =         {\n            city = anur;\n            \"consultation_fee\" = 458;\n            \"contact_no\" = 1234567898;\n            country = India;\n            id = 19;\n            \"organization_name\" = Danny;\n            \"pin_code\" = 411052;\n            \"reg_id\" = 0;\n            state = \"himachal pradesh\";\n            \"street_name\" = afafwfw;\n            \"wellness_flag\" = 0;\n        };\n        reg =         {\n            \"mobile_no\" = 0;\n            name = Batra;\n            \"profile_id\" = 0;\n            \"reg_id\" = 157;\n            \"wellness_id\" = 251215782521;\n        };\n    }",
    "\n        {\n        dp =         {\n            id = 0;\n            qualification = \"MD(Doctor)\";\n            \"reg_id\" = 0;\n            specialization1 = Orthopaedics;\n            specialization2 = Dermatologist;\n            specialization3 = Neurology;\n            url = \"www.batras.com\";\n        };\n        ds =         {\n            city = pabal;\n            \"consultation_fee\" = 500;\n            \"contact_no\" = 4512451252;\n            country = India;\n            id = 20;\n            \"organization_name\" = \"Org Name\";\n            \"pin_code\" = 411052;\n            \"reg_id\" = 0;\n            state = maharashtra;\n            \"street_name\" = afawafww;\n            \"wellness_flag\" = 0;\n        };\n        reg =         {\n            \"mobile_no\" = 0;\n            name = Batra;\n            \"profile_id\" = 0;\n            \"reg_id\" = 157;\n            \"wellness_id\" = 251215782521;\n        };\n    }",
    "\n        {\n        dp =         {\n            id = 0;\n            qualification = \"MD(Doctor)\";\n            \"reg_id\" = 0;\n            specialization1 = Orthopaedics;\n            specialization2 = Dermatologist;\n            specialization3 = Neurology;\n            specialization4 = Gynecologist;\n            url = \"danny.com\";\n        };\n        ds =         {\n            city = bumlitan;\n            \"consultation_fee\" = 504;\n            \"contact_no\" = 1234567898;\n            country = India;\n            id = 21;\n            \"organization_name\" = Danny;\n            \"pin_code\" = 411052;\n            \"reg_id\" = 0;\n            state = \"andaman and nicobar islands\";\n            \"street_name\" = \"East Street\";\n            \"wellness_flag\" = 0;\n        };\n        reg =         {\n            \"mobile_no\" = 0;\n            name = Danny;\n            \"profile_id\" = 0;\n            \"reg_id\" = 167;\n            \"wellness_id\" = 311220165848;\n        };\n    }",
    "\n        {\n        dp =         {\n            id = 0;\n            qualification = \"MD(Doctor)\";\n            \"reg_id\" = 0;\n            specialization1 = Orthopaedics;\n            specialization2 = Dermatologist;\n            specialization3 = Neurology;\n            specialization4 = Gynecologist;\n            url = \"danny.com\";\n        };\n        ds =         {\n            city = amtala;\n            \"consultation_fee\" = 451;\n            \"contact_no\" = 1234567898;\n            country = India;\n            id = 22;\n            \"organization_name\" = Danny;\n            \"pin_code\" = 411052;\n            \"reg_id\" = 0;\n            state = assam;\n            \"street_name\" = West;\n            \"wellness_flag\" = 0;\n        };\n        reg =         {\n            \"mobile_no\" = 0;\n            name = Danny;\n            \"profile_id\" = 0;\n            \"reg_id\" = 167;\n            \"wellness_id\" = 311220165848;\n        };\n    }",
    "\n        {\n        dp =         {\n            id = 0;\n            qualification = \"MD(Doctor)\";\n            \"reg_id\" = 0;\n            specialization1 = Orthopaedics;\n            specialization2 = Dermatologist;\n            specialization3 = Neurology;\n            specialization4 = Gynecologist;\n            url = \"danny.com\";\n        };\n        ds =         {\n            city = hala;\n            \"consultation_fee\" = 541;\n            \"contact_no\" = 1234567890;\n            country = India;\n            id = 23;\n            \"organization_name\" = \"Danny Clinic\";\n            \"pin_code\" = 411123;\n            \"reg_id\" = 0;\n            state = chandigarh;\n            \"street_name\" = Chandigarh;\n            \"wellness_flag\" = 0;\n        };\n        reg =         {\n            \"mobile_no\" = 0;\n            name = Danny;\n            \"profile_id\" = 0;\n            \"reg_id\" = 167;\n            \"wellness_id\" = 311220165848;\n        };\n    }\n)"
)

和内容:

content: (
        {
        dp =         {
            id = 0;
            qualification = "MD(Doctor)";
            "reg_id" = 0;
            specialization1 = Orthopaedics;
            specialization2 = Dermatologist;
            specialization3 = Neurology;
            url = "www.batras.com";
        };
        ds =         {
            city = agiripalle;
            "consultation_fee" = 450;
            "contact_no" = 1234567890;
            country = India;
            id = 18;
            "organization_name" = Batra;
            "pin_code" = 411052;
            "reg_id" = 0;
            state = "andhra pradesh";
            "street_name" = jhbfahjf;
            "wellness_flag" = 0;
        };
        reg =         {
            "mobile_no" = 0;
            name = Batra;
            "profile_id" = 0;
            "reg_id" = 157;
            "wellness_id" = 251215782521;
        };
    }
2016-12-21 13:52:48.184 Wellness_24x7[1223:55790] content: 
        {
        dp =         {
            id = 0;
            qualification = "MD(Doctor)";
            "reg_id" = 0;
            specialization1 = Orthopaedics;
            specialization2 = Dermatologist;
            specialization3 = Neurology;
            url = "www.batras.com";
        };
        ds =         {
            city = anur;
            "consultation_fee" = 458;
            "contact_no" = 1234567898;
            country = India;
            id = 19;
            "organization_name" = Danny;
            "pin_code" = 411052;
            "reg_id" = 0;
            state = "himachal pradesh";
            "street_name" = afafwfw;
            "wellness_flag" = 0;
        };
        reg =         {
            "mobile_no" = 0;
            name = Batra;
            "profile_id" = 0;
            "reg_id" = 157;
            "wellness_id" = 251215782521;
        };
    }
2016-12-21 13:52:48.185 Wellness_24x7[1223:55790] content: 
        {
        dp =         {
            id = 0;
            qualification = "MD(Doctor)";
            "reg_id" = 0;
            specialization1 = Orthopaedics;
            specialization2 = Dermatologist;
            specialization3 = Neurology;
            url = "www.batras.com";
        };
        ds =         {
            city = pabal;
            "consultation_fee" = 500;
            "contact_no" = 4512451252;
            country = India;
            id = 20;
            "organization_name" = "Org Name";
            "pin_code" = 411052;
            "reg_id" = 0;
            state = maharashtra;
            "street_name" = afawafww;
            "wellness_flag" = 0;
        };
        reg =         {
            "mobile_no" = 0;
            name = Batra;
            "profile_id" = 0;
            "reg_id" = 157;
            "wellness_id" = 251215782521;
        };
    }
2016-12-21 13:52:48.187 Wellness_24x7[1223:55790] content: 
        {
        dp =         {
            id = 0;
            qualification = "MD(Doctor)";
            "reg_id" = 0;
            specialization1 = Orthopaedics;
            specialization2 = Dermatologist;
            specialization3 = Neurology;
            specialization4 = Gynecologist;
            url = "danny.com";
        };
        ds =         {
            city = bumlitan;
            "consultation_fee" = 504;
            "contact_no" = 1234567898;
            country = India;
            id = 21;
            "organization_name" = Danny;
            "pin_code" = 411052;
            "reg_id" = 0;
            state = "andaman and nicobar islands";
            "street_name" = "East Street";
            "wellness_flag" = 0;
        };
        reg =         {
            "mobile_no" = 0;
            name = Danny;
            "profile_id" = 0;
            "reg_id" = 167;
            "wellness_id" = 311220165848;
        };
    }
2016-12-21 13:52:52.862 Wellness_24x7[1223:55790] content: 
        {
        dp =         {
            id = 0;
            qualification = "MD(Doctor)";
            "reg_id" = 0;
            specialization1 = Orthopaedics;
            specialization2 = Dermatologist;
            specialization3 = Neurology;
            specialization4 = Gynecologist;
            url = "danny.com";
        };
        ds =         {
            city = amtala;
            "consultation_fee" = 451;
            "contact_no" = 1234567898;
            country = India;
            id = 22;
            "organization_name" = Danny;
            "pin_code" = 411052;
            "reg_id" = 0;
            state = assam;
            "street_name" = West;
            "wellness_flag" = 0;
        };
        reg =         {
            "mobile_no" = 0;
            name = Danny;
            "profile_id" = 0;
            "reg_id" = 167;
            "wellness_id" = 311220165848;
        };
    }
2016-12-21 13:52:52.962 Wellness_24x7[1223:55790] content: 
        {
        dp =         {
            id = 0;
            qualification = "MD(Doctor)";
            "reg_id" = 0;
            specialization1 = Orthopaedics;
            specialization2 = Dermatologist;
            specialization3 = Neurology;
            specialization4 = Gynecologist;
            url = "danny.com";
        };
        ds =         {
            city = hala;
            "consultation_fee" = 541;
            "contact_no" = 1234567890;
            country = India;
            id = 23;
            "organization_name" = "Danny Clinic";
            "pin_code" = 411123;
            "reg_id" = 0;
            state = chandigarh;
            "street_name" = Chandigarh;
            "wellness_flag" = 0;
        };
        reg =         {
            "mobile_no" = 0;
            name = Danny;
            "profile_id" = 0;
            "reg_id" = 167;
            "wellness_id" = 311220165848;
        };
    }
)
2016-12-21 13:52:53.918 Wellness_24x7[1223:55790] content: (
        {
        dp =         {
            id = 0;
            qualification = "MD(Doctor)";
            "reg_id" = 0;
            specialization1 = Orthopaedics;
            specialization2 = Dermatologist;
            specialization3 = Neurology;
            url = "www.batras.com";
        };
        ds =         {
            city = agiripalle;
            "consultation_fee" = 450;
            "contact_no" = 1234567890;
            country = India;
            id = 18;
            "organization_name" = Batra;
            "pin_code" = 411052;
            "reg_id" = 0;
            state = "andhra pradesh";
            "street_name" = jhbfahjf;
            "wellness_flag" = 0;
        };
        reg =         {
            "mobile_no" = 0;
            name = Batra;
            "profile_id" = 0;
            "reg_id" = 157;
            "wellness_id" = 251215782521;
        };
    }

但是我的问题是,它没有显示在UITableViewCell

我尝试如下方式显示它:

[cell.drname setText:[[content objectForKey"dp"]valueForKey"qualification"]];  

还有 [cell.drname setText:[NSString stringWithFormat"%@", [[content objectForKey"dp"]valueForKey"qualification"]]];

但它给了我这样的错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7a07c600'

我没听懂,我错了。

请任何人都可以解决我的问题。帮助将是可观的。



Best Answer-推荐答案


您需要进行以下更改。

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:@"YourURL" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
    NSLog(@"JSON: %@", responseObject);

    NSArray *ResponseArray = (NSArray *)responseObject;
    if (ResponseArray.count >0)
    {
        _spclarr = [ResponseArray mutableCopy];
        NSLog(@"special_array : %@",_spclarr);

        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

        // saving Array to NSUserDefaults
        [prefs setObject:_spclarr forKey:@"spcl"];
        [prefs synchronize];
        [self performSegueWithIdentifier:@"doctorspcl" sender:self];

    }
} failure:^(NSURLSessionTask *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

详细 View Controller

-(void)viewDidLoad
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    //directly reading the array
    menuItems = [prefs objectForKey:@"spcl"];

}

你的 cellForRowAtIndexPath 应该是这样的

-(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSArray *menuItems = [prefs objectForKey:@"spcl"];
    NSDictionary *content = [menuItems objectAtIndex:indexPath.row];

    [cell.textLabel setText:[[content objectForKey:@"dp"]valueForKey:@"qualification"]];
    return cell;


}

当我们得到数组格式的响应时,为什么要将其存储在 NSUserDefaults 中作为 NSString ?要摆脱它,您需要直接在 NSUserDefaults 中存储一个数组并直接使用该数组。

关于ios - 如何在自定义 UITableViewCell 中的 Dictionary 中显示数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41257328/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap