从JSON数据填充UITableView

时间:2016-05-28 18:13:22

标签: objective-c json uitableview

我有JSON数据

[{"id": 1,"Name": "Nodia","company": "WWW","position": "HR","email": "sss@www.com","number": "33","photo": "image1.png"}, {"id": 2,"Name": "Nona ","company": "SSS","position": "Head of Department","email": "aaaa@samsda.ge","number": "5496","photo": "image2.png"}, {"id": 3,"Name": "David","company": "DDD","position": "Director","email": "test@sasd.e","number": "574","photo": "image3.png"},....]|

我创建了一个名为Name的表视图。

Tableviewcontroller.h :
     @interface CitiesViewController ()
        @property(nonatomic,strong)NSMutableArray *jsonArray
        @property(nonatomic,strong)NSMutableArray *citiesArray;
-(void)retrieveData;
Tableviewcontroller.m :
   @implementation CitiesViewController
        @synthesize 

citiesArray,jsonArray;

    #pragma mark -

    #pragma mark Class Methods

    -(void)retrieveData
    {

        NSURL *url = [NSURL URLWithString:getDataURL];
        NSData *data = [NSData dataWithContentsOfURL:url];

        jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

        citiesArray =[[NSMutableArray alloc]init];

        for (int i= 0; i<jsonArray.count; i++)
        {
            NSString *cID = [[jsonArray objectAtIndex:i] objectForKey:@"id"];
            NSString *cName = [[jsonArray objectAtIndex:i] objectForKey:@"Name"];
            NSString *cCompany = [[jsonArray objectAtIndex:i] objectForKey:@"company"];
            NSString *cPosition = [[jsonArray objectAtIndex:i] objectForKey:@"position"];
            NSString *cEmail = [[jsonArray objectAtIndex:i] objectForKey:@"email"];

            NSString *cNumber = [[jsonArray objectAtIndex:i] objectForKey:@"number"];
            NSString *cPhoto = [[jsonArray objectAtIndex:i] objectForKey:@"photo"];

            NSURL *urlOne = [NSURL URLWithString:cPhoto];
       //     NSLog(@"%@",urlOne);
            NSData *photoData = [NSData dataWithContentsOfURL:urlOne];
            UIImage *imageOne = [[UIImage alloc] initWithData:photoData];

            [citiesArray addObject:[[City alloc]initWithCityName:cName andCityState:cCompany andCityCountry:cEmail andCityPopulation:cPosition andCityID:cID andCityNumber:cNumber andCityPhoto: (UIImage *) imageOne]];

        }

        [self.tableView reloadData];

    }

    - (void)viewDidLoad {
        [super viewDidLoad];

       [self retrieveData];

    }

    #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
            return 1;
        }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
            return citiesArray.count;

    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

        // Configure the cell...


        City *cityob;
        cityob=[citiesArray objectAtIndex:indexPath.row];
        cell.textLabel.text=cityob.employeeName;


        return cell;

    }

一切正常.Cell显示数据。但问题是我无法在TableView中为section和section index标题设置标题标题。我想要分区的字母标题 因为数据是动态静态解决问题所不适合的。  like in this tutriole

非常感谢。原谅我糟糕的英语!

2 个答案:

答案 0 :(得分:0)

正如已经说过的,你需要实现tableView:titleForHeaderInSection:。您目前只有一个部分,并且不清楚您想要显示什么,但示例实现看起来像这样:

- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {

NSString *sectionNameString = @"Current Jobs";
return sectionNameString;}

在我的iPhone上输入此内容,但这大概就是它的样子。在类范围之外使用字符串比较函数,如下所示:

NSInteger sortCitiesByName(City *city1, City *city2, void *context) {
NSString *name1 = [city1 name];
NSString *name2 = [city2 name];
return [name1 localizedCaseInsensitiveCompare: name2];}

然后按照以下方式对数组进行排序:

citiesArray = [citiesArray sortedArrayUsingFunction:sortCitiesByName context:nil];

答案 1 :(得分:0)

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
       return [citiesArray objectAtIndex:section];
    }

请检查一下并做出适当的修改。