【发布时间】:2016-05-28 18:13:21
【问题描述】:
我有 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;
}
一切正常。单元格显示数据。但问题是我无法在 TableView 中为部分和部分索引标题设置标题标题。我想要部分的字母标题 因为数据是动态的,静态的解决问题是不合适的。 like in this tutriole
非常感谢。请原谅我的英语不好!
【问题讨论】:
-
虽然这是 Swift 版本的代码,但您应该使用
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { },它是UITableViewDelegate的扩展。可以参考this。 -
@ama 再详细一点?
-
@amagain - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { } 但我不知道如何实现
标签: objective-c json uitableview