1

我对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
4

1 回答 1

0

它无法正常工作,因为在您的prepareForSegue:方法中,您正在检查所选行:

NSIndexPath *path = [self.tableView indexPathForSelectedRow];
RealMadrid *c = [players objectAtIndex:path.row];

但是您没有检查该部分。因此,当您在第一部分中点击第二行时,path.row将与您在第二部分中点击第二行相同。

我建议players完全删除数组,并检查方法中的部分prepareForSegue:,就像在所有其他方法中一样。(要确定,您应该从哪个列表中拉出您的RealMadrid对象。)

于 2013-11-01T11:34:55.233 回答