Iphone UITableView分组数组
我似乎无法从逻辑上理解如何为每个分组的部分使用一个单独的数组,以便在导航控制器的详细视图中显示不同的数据。对于每个组,都有不同的数据,但第二个视图当前在UILabel中显示来自一个数组的数据,并从每个组的数组开头开始。如果你需要更多信息,请告诉我Iphone UITableView分组数组,iphone,ios,objective-c,arrays,uitableview,Iphone,Ios,Objective C,Arrays,Uitableview,我似乎无法从逻辑上理解如何为每个分组的部分使用一个单独的数组,以便在导航控制器的详细视图中显示不同的数据。对于每个组,都有不同的数据,但第二个视图当前在UILabel中显示来自一个数组的数据,并从每个组的数组开头开始。如果你需要更多信息,请告诉我 - (void)viewDidLoad { [super viewDidLoad]; //Keeping of Business Names for Detailed View addressBook=@[@"Clinic 1
- (void)viewDidLoad
{
[super viewDidLoad];
//Keeping of Business Names for Detailed View
addressBook=@[@"Clinic 1 Business",@"Clinic 2 Business",@"Clinic 3 Business"];
//holding of clinic names per county
adams=@[@"Clinic 1 Business Listing",@"Clinic 2 Business Listing",@"eeeee"];
allegheny=@[@"Clinic 3 Business Listing",@"Clinic 4 Business Listing"];
armstrong=@[@"Clinic 5 Business Listing",@"Clinic 6 Business Listing",@"Clinic 7 Business Listing"];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
NSLog(@"this is the content of addressBook: \n %@", addressBook);
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
//return addressBook.count;
if(section==0)
{
return [adams count];
}
else if(section==1)
{
return [allegheny count];
}
else if (section==2)
{
return [armstrong count];
}
}
/*
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return [NSArray arrayWithObjects:@"Rec", @"A", @"B", nil ];
}
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"abCell";
addressBookCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(indexPath.section==0)
{
cell.addressBookLbl.text=[adams objectAtIndex:indexPath.row];
}
else if(indexPath.section==1)
{
cell.addressBookLbl.text=[allegheny objectAtIndex:indexPath.row];
}
else if (indexPath.section==2)
{
cell.addressBookLbl.text=[armstrong objectAtIndex:indexPath.row];
}
return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier]isEqualToString:@"addressBookDetails"]) {
addressBookDetailsViewController *addressDetailsController = [segue destinationViewController];
NSIndexPath *abIndexPath = [self.tableView indexPathForSelectedRow];
int row = [abIndexPath row];
addressDetailsController.addressDetail=@[addressBook[row]];
NSLog(@"this is the content of addressBook: \n %@", addressBook);
}
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section==0) {
return @"Adams County";
}
else if(section==1)
{
return @"Allegheny County";
}
else if(section==2)
{
return @"Armstrong County";
}
}
我对您试图使用上面的
addressBook
数组做什么感到有点困惑。剩下的,看起来你应该用字典来解释。你熟悉字典吗?您可以为该县将键设置为county,将值设置为array。然后,当您在表格视图中请求numberofsection
时,您返回[myDictionary allKeys]。count
让我知道这是否有意义。如果没有,我将进一步阐述。另外,请描述地址簿的用途。传递数据的方式是使用segue,这就是您正在做的。您没有正确执行的操作是在
prepareforsgue
方法中。为了简化操作,请使用didSelectRowAtIndexPath
方法,这样您就不会创建自己查找所选单元格的方法
要处理这个问题,您必须在didSelectRowAtIndexPath
中添加一点。以下是您需要做的:
委员会:
您拥有的方法应如下所示:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"addressBookDetails"]) {
addressBookDetailsViewController *addressDetailsController = [segue destinationViewController];
addressDetailsController.addressDetail = selectedAddressBook;
//selectedAddressBook is an array that you don't have yet, but will create in the next step
//Make sure that the addressDetail in the DetailViewController is an Array, and not something else
}
}
现在您要创建数组selectedAddressBook
。因此在标题中(我们仍然在MasterViewController中):
好,现在回到实现文件(仍然是MasterViewController)。现在,您将使用didSelectRowAtIndexPath
方法(最后我将解释此方法的重要性)
如果您现在尝试运行它,它可能会崩溃。删除现有的segue。删除它就行了。在MasterViewController本身和DetailViewController之间创建一个新的分段。选择您想要的任何类型,但您可能应该使用Replace
segue。为该序列指定与刚删除的序列相同的标识符
现在,在DetailViewController.m文件中放置一个NSLog以监视viewdiload
中的数据:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Did the array get passed along? Let's see :\n %@", addressDetail);
//addressDetail should be the name of the array in this VC
}
如果一切顺利,那么日志应该只显示所选行的详细信息。现在,在DetailViewController中,您可以访问阵列中的数据,并根据需要进行显示
在使用tableView时,您需要使用
didSelectRowAtIndexPath
方法的原因实际上是因为它使生活变得简单。此方法使用nsindepath识别选定的行。这就消除了你在prepareForSegue
方法中观察它的方式它告诉代理指定的行现在已被选中。使用它,您可以有选择地对单元格点击执行操作。不确定您想要实现什么。你解释了问题,但不是理想的场景。理想的目标是在表中每个部分的详细视图中显示不同的数据。didSelectRowAtIndexPath
中有什么?没有什么,我不知道该怎么处理。:哦,我的朋友,这就是你将如何传递数据的方式!你怎么称呼addressBookDetails segue?它和原型电池相连吗?我不太熟悉字典。请进一步解释。addressBook只是一个数组,用于保存要在详细视图中显示的业务名称。您应该使用NxDictionNary(哈希表,如果您愿意:)我不知道如何使用字典实现这一点。如何“替换”segue?@Klinetel,“替换”segue是一种segue类型。删除当前视图Ctrl+单击并拖动到下一个视图,将出现一个弹出窗口。选择“替换”。运气不好,我不明白你的意思。我甚至在谷歌上看了看也没有找到答案。我尝试了你解释的没有替换segue(刚刚创建了一个新的)的东西,现在当我选择一个表行时它崩溃了……你使用了哪个segue,崩溃的意思是@Klinetel?
@interface //blah blah blah
{
NSArray *selectedAddressBook;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//some animation to fade away the blue on selection
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//here you will populate the array that you created
selectedAddressBook = (NSArray *)addressBook[indexPath.row];
//now you perform your segue
[self performSegueWithIdentifier:@"addressBookDetails" sender:self];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Did the array get passed along? Let's see :\n %@", addressDetail);
//addressDetail should be the name of the array in this VC
}