UITableView在每个部分返回相同的项目

时间:2014-03-24 19:34:08

标签: ios nsindexpath

我创建了一个包含剖面的表格。每个部分都有一个日期(2014-03-23)作为标题,并且在每个日期下我想要填充当天要播放的游戏列表。当我运行应用程序时,表格获得了部分标题(游戏日期),但每个部分都有相同的匹配列表。我希望在比赛日期之前进行比赛。 我知道我需要在CellForRowsAtIndexPath中包含indexPath.section,但我很难搞清楚它。

这是我的代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
            {

                return gamesArray.count;
            }


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
        {

            uniqueArray = [[NSOrderedSet orderedSetWithArray:dateSection] array];
            return [uniqueArray count];



        }


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
        {
            return [self.dateSection objectAtIndex:section];
        }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         //NSString *CellIdentifier = [NSString stringWithFormat:@"games cell-%ld-%ld", (long)indexPath.section, (long)indexPath.row];
        static NSString *CellIdentifier = @"games cell";
        //NSString *CellIdentifier = [NSString stringWithFormat:@"cell-%d-%d", indexPath.section, indexPath.row];


        CustomInboxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = (CustomInboxCell *)[[CustomInboxCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        PFObject *post = [gamesArray objectAtIndex:indexPath.row]; 
        [cell.teamsLabel setText:[post  objectForKey:@"teams"]];
        [cell.liveRepeatLabel setText:[post objectForKey:@"liveRepeat"]];
        [cell.gameTimeLabel setText:[post objectForKey:@"gameTime"]];

        return cell;
    }

非常感谢任何帮助。

//======================================================
//I decided to use a predicate to filter and get the number of items per date(Number of games per date)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    NSString *sectionTitle = [uniqueArray objectAtIndex:section];

    if (section >=0) {

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"gameDate == %@",sectionTitle];
        NSLog(@"section name = %@", sectionTitle);
        NSArray *filtered = [gamesArray filteredArrayUsingPredicate:predicate];
        NSLog(@"filtered = %@",filtered);
        return filtered.count;
    }

    return 0;
}

//我只需要遍历每个日期并返回每个日期的游戏数量。有什么建议吗?

2 个答案:

答案 0 :(得分:2)

表格的每个部分都需要一个单独的数组。在numberOfRowsForSection中,您需要为与给定count对应的数组返回section


这是一个例子。该表的数据存储在名为tableData的NSArray中。该数组为表的每个部分都有一个条目。 tableData中的每个条目都是NSDictionary。 NSDictionary有两个键,titleitemstitle键对应于NSString,该NSString用作表部分的标题。 items键对应于具有表部分行信息的NSArray。

该表分为两个部分,如

Fruits
    Apples
    Oranges
Animals
    Dog
    Cat
    Horse
    Cow

这是代码

#import "MainViewController.h"

@interface MainViewController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *tableData;
@end

@implementation MainViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.dataSource = self;
    self.tableView.delegate = self;

    NSDictionary *fruits, *animals;

    fruits  = @{ @"title" : @"Fruits" , @"items" : @[@"Apples", @"Oranges"]            };
    animals = @{ @"title" : @"Animals", @"items" : @[@"Dog", @"Cat", @"Horse", @"Cow"] };

    self.tableData = @[fruits, animals];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return( self.tableData.count );
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *sectionData = self.tableData[section];
    NSArray *items = sectionData[@"items"];
    return( items.count );
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSDictionary *sectionData = self.tableData[section];
    NSString *title = sectionData[@"title"];
    return( title );
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SomeCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    NSDictionary *sectionData = self.tableData[indexPath.section];
    NSArray *items = sectionData[@"items"];
    NSString *name = items[indexPath.row];

    cell.textLabel.text = name;

    return cell;
}

@end

答案 1 :(得分:0)

这是一个数据集问题,您需要为tableview的每个部分准备一个单独的数据集,并使用cellForRowAtIndexPath方法中的索引路径(行,部分)属性对它们进行迭代。如果您可以执行NSLog并共享数据集,那么准确回答会更有帮助。希望这会有所帮助。