我是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
答案 0 :(得分:0)
它没有正常工作,因为在prepareForSegue:
方法中,您正在检查所选行:
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
RealMadrid *c = [players objectAtIndex:path.row];
但你不检查该部分。因此,当您点按第一部分的第二行时,path.row
将与您在第二部分部分中点击第二行的情况相同。
我建议完全删除players
数组,并检查prepareForSegue:
方法中的部分,就像在其他所有方法中一样。 (要确定,应从哪个列表中提取RealMadrid
对象。)