Iphone 如何用一个数组填充多个tableview节
我正在开发一个显示公司数据的tableview。我想将数据分为3个部分,使其看起来更有条理 公司的数据是从mysql数据库检索的,我在一个数组中接收到它,如下所示:Iphone 如何用一个数组填充多个tableview节,iphone,objective-c,xcode4.5,uitableview,Iphone,Objective C,Xcode4.5,Uitableview,我正在开发一个显示公司数据的tableview。我想将数据分为3个部分,使其看起来更有条理 公司的数据是从mysql数据库检索的,我在一个数组中接收到它,如下所示: { companyAdress = "the street 9"; companyCity = "city"; companyFacebook = "facebook.com/companyname"; companyName = "name"; companyPhoneNumber = "0
{
companyAdress = "the street 9";
companyCity = "city";
companyFacebook = "facebook.com/companyname";
companyName = "name";
companyPhoneNumber = "0123 456 789";
companyTwitter = "www.twitter.com/companyname";
companyWebsite = "www.companyname.com";
companyZip = "0000 AA";
imageNumber = "3067913";
}
我希望companyName
和imageNumber
在第一部分,companyAddress
,companyZip
和companyCity
在第二部分,所有剩余变量在第三部分
我不知道如何正确地做到这一点,而且我还没有在SO或我知道的任何其他网站上找到一个有用的答案/解决方案
我该怎么做?任何帮助、示例代码和/或教程都将不胜感激,提前谢谢 一种方法是将接收到的数据分离成二维数组。因此,数组的第一个条目将是一个包含
companyName
和imageNumber
的数组,依此类推
在这个实现中,numberOfSectionsInTableView
将只返回myArray.count
和numberofrowsinssection
将返回myArray[section]。count
要从那里访问适当的值,您可以执行类似于
((NSMutableArray*)myArray[indexpath.section])[indexpath.row]
的操作,您必须使用NSDictionary项的数组,
然后,您将获得节和表行的信息。
为每个记录类型添加一个键
这是一个示例项目,用于解释NSArray和NSDictionary的使用,希望对您有所帮助
您可以从这里下载xcode项目,这是示例的结果
#导入
@接口FGViewController:UIViewController{
NSMutableArray*记录;
}
@属性(非原子,强)IBUITableView*tblRecordsList;
-(void)buildDataSource//为tableview构建数据源
@结束
#导入“FGViewController.h”
@接口FGViewController()
@结束
@视图控制器的实现
@合成tblRecordsList;
-(无效)viewDidLoad
{
[超级视图下载];
记录=[[NSMutableArray alloc]init];
//将数据加载到用作数据源的数组中
[自建数据源];
self.tblRecordsList.dataSource=self;
self.tblRecordsList.delegate=self;
}
-(无效)未收到记忆警告
{
[超级记忆警告];
}
//加载样本数据
-(void)buildDataSource{
NSMutableDictionary*dict=nil;
[记录删除所有对象];
//用数据填充数据源
//要放在第一节上的数据
dict=[NSMutableDictionary字典,容量:0];
[dict setObject:@“0”forKey:@“idsection”];
[dict setObject:@“company1”forKey:@“companyName”];
[dict setObject:@“picture1”forKey:@“imageNumber”];
[records addObject:dict];//将项添加到数组
//第二部分的数据
dict=[NSMutableDictionary字典,容量:0];
[dict setObject:@“1”forKey:@“idsection”];
[dict setObject:@“address1”forKey:@“CompanyAddress”];
[dict setObject:@“zip1”forKey:@“companyZip”];
[dict setObject:@“city1”forKey:@“companyCity”];
[records addObject:dict];//将项添加到数组
//要放在其他部分上的数据
dict=[NSMutableDictionary字典,容量:0];
[dict setObject:@“2”forKey:@“idsection”];
[dict setObject:@“facebook1”forKey:@“companyFacebook”];
[dict setObject:@“phone1”forKey:@“companyPhoneNumber”];
[dict setObject:@“twitter1”forKey:@“companywitter”];
[dict setObject:@“网站1”forKey:@“公司网站”];
[records addObject:dict];//将项添加到数组
}
//使用节键获取字典(idsection)
-(NSDictionary*)词典分册:(NSInteger)分册{
用于(NSDictionary*记录中的dict){
如果(节==[[dict valueForKey:@“idsection”]intValue]){
返回命令;
}
}
返回零;
}
//表视图委托
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*单元格=nil;
cell=[tableView dequeueReusableCellWithIdentifier:@“myCellReuseID”];
NSDictionary*dict=[self dictionaryForSection:indexPath.section];//获取请求字典信息
//第一部分的工艺数据
if(indexPath.section==0){
if(indexPath.row==0)
cell.textlab.text=[dict valueForKey:@“companyName”];
if(indexath.row==1)
cell.textlab.text=[dict valueForKey:@“imageNumber”];
}
//第二部分的工艺数据
if(indexPath.section==1){
if(indexPath.row==0)
cell.textlab.text=[dict valueForKey:@“companyAddress”];
if(indexath.row==1)
cell.textlab.text=[dict valueForKey:@“companyZip]”;
if(indexPath.row==2)
cell.textlab.text=[dict valueForKey:@“companyCity”];
}
//其他部分的过程数据
if(indexPath.section==2){
if(indexPath.row==0)
cell.textlab.text=[dict valueForKey:@“companyFacebook”];
if(indexath.row==1)
cell.textlab.text=[dict valueForKey:@“companyPhoneNumber”];
if(indexPath.row==2)
cell.textlab.text=[dict valueForKey:@“companywitter”];
if(indexPath.row==3)
cell.textlab.text=[dict valueForKey:@“companyWebsite”];
}
返回单元;
}
//节数(第一节、第二节和其他节=>3)
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回3;
}
-(NSString*)表格视图:(UITableView*)标题标题部分:(NSInteger)部分
{
NSString*sectionTitle=@”;
道岔(区段){
案例0:
sectionTitle=@“标题第一节”;
打破
案例1:
sectionTitle=@“第二节标题”;
打破
案例2:
sectionTitle=@“标题其他部分”;
打破
违约:
打破
}
返回标题;
}
#import <UIKit/UIKit.h>
@interface FGViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> {
NSMutableArray* records;
}
@property(nonatomic,strong) IBOutlet UITableView *tblRecordsList;
-(void) buildDataSource; //Build the datasource for the tableview
@end
#import "FGViewController.h"
@interface FGViewController ()
@end
@implementation FGViewController
@synthesize tblRecordsList;
- (void)viewDidLoad
{
[super viewDidLoad];
records = [[NSMutableArray alloc] init];
//Load data into array used as datasource
[self buildDataSource];
self.tblRecordsList.dataSource=self;
self.tblRecordsList.delegate=self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
//Load sample data
-(void) buildDataSource{
NSMutableDictionary* dict= nil;
[records removeAllObjects];
//Fill data source with your data
//Data to put on first section
dict=[NSMutableDictionary dictionaryWithCapacity:0];
[dict setObject:@"0" forKey:@"idsection"];
[dict setObject:@"company1" forKey:@"companyName"];
[dict setObject:@"picture1" forKey:@"imageNumber"];
[records addObject:dict]; //Add items to array
//Data to put on second section
dict=[NSMutableDictionary dictionaryWithCapacity:0];
[dict setObject:@"1" forKey:@"idsection"];
[dict setObject:@"address1" forKey:@"companyAdress"];
[dict setObject:@"zip1" forKey:@"companyZip"];
[dict setObject:@"city1" forKey:@"companyCity"];
[records addObject:dict]; //Add items to array
//Data to put on other section
dict=[NSMutableDictionary dictionaryWithCapacity:0];
[dict setObject:@"2" forKey:@"idsection"];
[dict setObject:@"facebook1" forKey:@"companyFacebook"];
[dict setObject:@"phone1" forKey:@"companyPhoneNumber"];
[dict setObject:@"twitter1" forKey:@"companyTwitter"];
[dict setObject:@"website1" forKey:@"companyWebsite"];
[records addObject:dict]; //Add items to array
}
//Get Dictionary using section key (idsection)
-(NSDictionary *) dictionaryForSection:(NSInteger) section{
for (NSDictionary *dict in records){
if(section == [[dict valueForKey:@"idsection"] intValue]){
return dict;
}
}
return nil;
}
//Table View Delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell =nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"myCellReuseID"];
NSDictionary * dict = [self dictionaryForSection:indexPath.section]; //Get request dictionary info
//Process data for first section
if(indexPath.section == 0){
if(indexPath.row == 0)
cell.textLabel.text=[dict valueForKey:@"companyName"];
if(indexPath.row == 1)
cell.textLabel.text=[dict valueForKey:@"imageNumber"];
}
//Process data for second section
if(indexPath.section == 1){
if(indexPath.row == 0)
cell.textLabel.text=[dict valueForKey:@"companyAdress"];
if(indexPath.row == 1)
cell.textLabel.text=[dict valueForKey:@"companyZip"];
if(indexPath.row == 2)
cell.textLabel.text=[dict valueForKey:@"companyCity"];
}
//Process data for other section
if(indexPath.section == 2){
if(indexPath.row == 0)
cell.textLabel.text=[dict valueForKey:@"companyFacebook"];
if(indexPath.row == 1)
cell.textLabel.text=[dict valueForKey:@"companyPhoneNumber"];
if(indexPath.row == 2)
cell.textLabel.text=[dict valueForKey:@"companyTwitter"];
if(indexPath.row == 3)
cell.textLabel.text=[dict valueForKey:@"companyWebsite"];
}
return cell;
}
//Number of sections (first,second and other => 3)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSString *)tableView:(UITableView *)theTableView titleForHeaderInSection:(NSInteger)section
{
NSString * sectionTitle =@"";
switch (section) {
case 0:
sectionTitle = @"title first section";
break;
case 1:
sectionTitle = @"title second section";
break;
case 2:
sectionTitle = @"title other section";
break;
default:
break;
}
return sectionTitle;
}
//Count number of record for sections
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int nRecords=0;
int idSection =0;
//Count number of items for specified section
for (NSDictionary *dict in records){
idSection = [[dict valueForKey:@"idsection"] intValue];
if(section == idSection){
nRecords = [[dict allKeys] count] -1 ; //All dictionary Keys - 1 (the first key "idsection")
}
}
return nRecords;
}
@end