OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在自定义 UITableViewCell 中的 Dictionary 中显示数据? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 14:29
标题: ios - 如何在自定义 UITableViewCell 中的 Dictionary 中显示数据?

我正在使用 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4