![](/img/trans.png)
[英]Passing NSMutableArray dictionary data to another view using segue
[英]Passing NSMutableArray data to another view using a grouped UITableViewController
我对iOS开发很陌生。 我有一个应用程序,其中包含使用UITableViewController
的玩家列表。
点击名称时,它将把您传递给另一个具有更多详细信息的ViewController
。
我将数据存储在NSMutableArray
。 表格视图很好,问题在于,当我从另一部分点击播放器时,数据将从列表顶部开始加载。
======================
第1节 (2行)
玩家1
玩家2
第2节 (8行)
玩家1
玩家2
...
玩家8
第1节 (2行)
玩家1
玩家2
第2节 (8行)
玩家3
玩家4
...
玩家10
希望您能理解,感谢您的帮助,这让我发疯了!!
#import "ListOfPlayersViewController.h"
@interface ListOfPlayersViewController ()
@end
@implementation ListOfPlayersViewController
@synthesize goalies, defenders, midfielders, forwards, players;
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"ShowPhoto"]) {
PlayersInfoViewController *dvc = [segue destinationViewController];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
RealMadrid *c = [players objectAtIndex:path.row];
[dvc setCurrentPlayer:c];
}
}
- (id)initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
//goalkeepers
goalies = [[NSMutableArray alloc] init];
midfielders = [[NSMutableArray alloc] init];
forwards = [[NSMutableArray alloc] init];
defenders = [[NSMutableArray alloc] init];
RealMadrid *playas = [[RealMadrid alloc]init];
[playas setName:@"Iker Casillas"];
[playas setPhoto:@"Iker.jpg"];
[playas setInfo:
@"Goalkeeper, Number 1, Legend, Spanish"];
[goalies addObject:playas];
playas = [[RealMadrid alloc]init];
[playas setName:@"Diego López"];
[playas setPhoto:@"Diego.jpeg"];
[playas setInfo:
@"Goalkeeper, Number 25, In-Form, Spanish"];
[goalies addObject:playas];
//defenders
playas = [[RealMadrid alloc]init];
[playas setName:@"Daniel Carvajal"];
[playas setPhoto:@"Dani.jpg"];
[playas setInfo:
@"Fullback, Number 15, Fast, Spanish"];
[defenders addObject:playas];
playas = [[RealMadrid alloc]init];
[playas setName:@"Álvaro Arbeloa"];
[playas setPhoto:@"Arbeloa.jpg"];
[playas setInfo:
@"Fullback, Number 17, Clever, Spanish"];
[defenders addObject:playas];
playas = [[RealMadrid alloc]init];
[playas setName:@"Pepe"];
[playas setPhoto:@"Pepe.jpg"];
[playas setInfo:
@"Defender, Number 3, Aggressive, Portuguese"];
[defenders addObject:playas];
playas = [[RealMadrid alloc]init];
[playas setName:@"Sergio Ramos"];
[playas setPhoto:@"Sergio.jpg"];
[playas setInfo:
@"Defender, Number 4, Leader, Spanish"];
[defenders addObject:playas];
playas = [[RealMadrid alloc]init];
[playas setName:@"Raphaël Varane"];
[playas setPhoto:@"Varane.jpg"];
[playas setInfo:
@"Defender, Number 2, Clever, French"];
[defenders addObject:playas];
playas = [[RealMadrid alloc]init];
[playas setName:@"Nacho Fernández"];
[playas setPhoto:@"Nacho.jpeg"];
[playas setInfo:
@"Defender, Number 27, Young, Spanish"];
[defenders addObject:playas];
playas = [[RealMadrid alloc]init];
[playas setName:@"Marcelo"];
[playas setPhoto:@"Marcelo.jpg"];
[playas setInfo:
@"Fullback, Number 12, Dribbler, Brazilian"];
[defenders addObject:playas];
playas = [[RealMadrid alloc]init];
[playas setName:@"Fábio Coentrão"];
[playas setPhoto:@"Fabio.jpg"];
[playas setInfo:
@"Fullback, Number 5, Hardworking, Portuguese"];
[defenders addObject:playas];
//midfielders
playas = [[RealMadrid alloc]init];
[playas setName:@"Isco"];
[playas setPhoto:@"Isco.jpg"];
[playas setInfo:
@"Midfielder, Number 23, Clever, Spanish"];
[midfielders addObject:playas];
playas = [[RealMadrid alloc]init];
[playas setName:@"Ángel di María"];
[playas setPhoto:@"Angel.jpg"];
[playas setInfo:
@"Winger, Number 22, Fast, Argetinian"];
[midfielders addObject:playas];
playas = [[RealMadrid alloc]init];
[playas setName:@"Sami Khedira"];
[playas setPhoto:@"Sami.jpeg"];
[playas setInfo:
@"Midfielder, Number 6, Powerful, German"];
[midfielders addObject:playas];
playas = [[RealMadrid alloc]init];
[playas setName:@"Xabi Alonso"];
[playas setPhoto:@"Xabi.jpeg"];
[playas setInfo:
@"Midfielder, Number 14, Clever, Spanish"];
[midfielders addObject:playas];
playas = [[RealMadrid alloc]init];
[playas setName:@"Luka Modrić"];
[playas setPhoto:@"Luka.jpeg"];
[playas setInfo:
@"Midfielder, Number 19, Clever, Croatian"];
[midfielders addObject:playas];
playas = [[RealMadrid alloc]init];
[playas setName:@"Casemiro"];
[playas setPhoto:@"Casemiro.jpeg"];
[playas setInfo:
@"Midfielder, Number 38, Strong, Brazilian"];
[midfielders addObject:playas];
playas = [[RealMadrid alloc]init];
[playas setName:@"Asier Illaramendi"];
[playas setPhoto:@"Illara.jpeg"];
[playas setInfo:
@"Midfielder, Number 24, Clever, Spanish"];
[midfielders addObject:playas];
//forwards
playas = [[RealMadrid alloc]init];
[playas setName:@"Cristiano Ronaldo"];
[playas setPhoto:@"Cristiano.jpg"];
[playas setInfo:
@"Left Wing, Number 7, Portuguese, Very fast and powerful"];
[forwards addObject:playas];
playas = [[RealMadrid alloc]init];
[playas setName:@"Karim Benzema"];
[playas setPhoto:@"Karim.jpeg"];
[playas setInfo:
@"Forward, Number 9, French, Good shoot"];
[forwards addObject:playas];
playas = [[RealMadrid alloc]init];
[playas setName:@"Álvaro Morata"];
[playas setPhoto:@"Morata.jpg"];
[playas setInfo:
@"Forward, Number 21, Young, Spanish"];
[forwards addObject:playas];
playas = [[RealMadrid alloc]init];
[playas setName:@"Jesé"];
[playas setPhoto:@"Jese.jpg"];
[playas setInfo:
@"Forward, Number 20, Fast, Spanish"];
[forwards addObject:playas];
playas = [[RealMadrid alloc]init];
[playas setName:@"Gareth Bale"];
[playas setPhoto:@"Gareth.jpg"];
[playas setInfo:
@"Winger, Number 11, Fast, Welsh"];
[forwards addObject:playas];
players = [[NSMutableArray alloc]init];
[players addObjectsFromArray:goalies];
[players addObjectsFromArray:defenders];
[players addObjectsFromArray:midfielders];
[players addObjectsFromArray:forwards];
[self.tableView reloadData];
}
- (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 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0:
return goalies.count;
break;
case 1:
return defenders.count;
break;
case 2:
return midfielders.count;
break;
default:
return forwards.count;
break;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
switch (section) {
case 0:
return @"Goalkeepers";
break;
case 1:
return @"Defenders";
break;
case 2:
return @"Midfielders";
break;
default:
return @"Forwards";
break;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PhotoCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell ==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
RealMadrid *currentPlayer;
if ([indexPath section] == 0) {
currentPlayer = [goalies objectAtIndex:indexPath.row];
}
if ([indexPath section] == 1) {
currentPlayer = [defenders objectAtIndex:indexPath.row];
}
if ([indexPath section] == 2) {
currentPlayer = [midfielders objectAtIndex:indexPath.row];
}
if ([indexPath section] == 3){
currentPlayer = [forwards objectAtIndex:indexPath.row];
}
[cell.textLabel setText:currentPlayer.name];
return cell;
}
@end
它无法正常工作,因为在您的prepareForSegue:
方法中,您正在检查选定的行:
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
RealMadrid *c = [players objectAtIndex:path.row];
但是您没有检查该部分。 因此,当您点击第一部分的第二行时, path.row
将与您点击第二部分的第二行相同。
我建议完全删除players
数组,并像其他所有操作一样检查您的prepareForSegue:
方法中的部分。 (要确定从哪个列表中拉出RealMadrid
对象。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.