UITableViewCell字幕无法从数组中正确更新

时间:2015-07-24 02:00:11

标签: ios objective-c iphone arrays uitableview

我目前正在开发一个简单的锻炼跟踪器应用程序,我有一个桌面视图,每个锻炼计划数据都会加载到每个细胞上。

我使用字幕作为我在故事板中选择的tableview单元格样式。

我将每个练习名称放在文本标签上,并将详细信息和次数设置到详细标签上。

我的问题是练习数据相对于训练阵列正确加载到每一行的文本标签,但代表和&设置信息未正确加载。

Week1_3ViewController.m

#import "Week1_3ViewController.h"
#import "SWRevealViewController.h"

@interface Week1_3ViewController ()

@property (nonatomic) NSMutableArray *workouts;
@property (nonatomic) NSArray *reps;
@property (nonatomic) NSArray *days;

@end

@implementation Week1_3ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;

    self.workouts = @[@{@"name" : @"SQUATS", @"days" : @"DAY 1"}, @{@"name" : @"BENT OVER ROWS", @"days" : @"DAY 1"}, @{@"name" : @"BENCH PRESS", @"days" : @"DAY 1"}, @{@"name" : @"OVERHEAD PRESS", @"days" : @"DAY 1"}, @{@"name" : @"EXTERNAL ROTATIONS", @"days" : @"DAY 1"}, @{@"name" : @"SEATED CALF RAISES", @"days" : @"DAY 1"}, @{@"name" : @"MOUNTAIN CLIMBERS", @"days" : @"DAY 1"}, @{@"name" : @"PLANKS", @"days" : @"DAY 1"}, @{@"name" : @"DEADLIFT", @"days" : @"DAY 2"}, @{@"name" : @"KNEELING LANDMINE PRESS", @"days" : @"DAY 2"}, @{@"name" : @"ALT. ARNOLD PRESS", @"days" : @"DAY 2"}, @{@"name" : @"ALT. FRONT LUNGE", @"days" : @"DAY 2"}, @{@"name" : @"PULL OVER", @"days" : @"DAY 2"}, @{@"name" : @"WEIGHTED CRUNCHES", @"days" : @"DAY 2"}, @{@"name" : @"SIDE PLANKS", @"days" : @"DAY 2"}, @{@"name" : @"FRONT SQUAT", @"days" : @"DAY 3"}, @{@"name" : @"T-BAR ROW", @"days" : @"DAY 3"}, @{@"name" : @"DIPS", @"days" : @"DAY 3"}, @{@"name" : @"UPRIGHT ROW", @"days" : @"DAY 3"}, @{@"name" : @"GLUTE BRIDGES (WEIGHTED)", @"days" : @"DAY 3"}, @{@"name" : @"STANDING CALF RAISES", @"days" : @"DAY 3"}, @{@"name" : @"RUSSIAN TWIST", @"days" : @"DAY 3"}].mutableCopy;

    self.reps = @[@"15, 12, 10, 8, 6", @"12, 10, 8, 6, 6", @"15, 12, 10, 8, 6", @"12, 10, 8, 6, 6", @"3 x 12", @"3 x 15", @"3 x 30 SECONDS", @"3 x 30 SECONDS", @"15, 12, 10, 8, 6", @"15, 12, 10, 8, 8", @"12, 10, 8, 8, 6", @"5 x 10", @"3 x 15", @"3 x 15", @"3 x 30 SECONDS", @"15, 12, 10, 8, 6", @"12, 10, 8, 8, 6", @"5 x 15", @"15, 12, 10, 8, 8", @"3 x 10", @"3 x 15", @"3 x 30 (15 EACH SIDE)"];
    self.days = @[@"DAY 1", @"DAY 2", @"DAY 3"];


    self.title = @"WEEK 1-3, PHASE 1";

    SWRevealViewController *revealViewController = self.revealViewController;
    if ( revealViewController )
    {
        [self.sidebarButton setTarget: self.revealViewController];
        [self.sidebarButton setAction: @selector( revealToggle: )];
        [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - DataSource helper methods

- (NSArray *) itemsInDays:(NSString *)targetDays {
    NSPredicate *matchingPredicate = [NSPredicate predicateWithFormat:@"days == %@", targetDays];
    NSArray *daysItems = [self.workouts filteredArrayUsingPredicate:matchingPredicate];

    return daysItems;
}

- (NSInteger)numberOfItemsInDays:(NSString *)targetDays {
    return [self itemsInDays:targetDays].count;
}

- (NSDictionary *)itemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *days = self.days[indexPath.section];
    NSArray *daysItems = [self itemsInDays:days];
    NSDictionary *item = daysItems[indexPath.row];

    return item;
}

- (NSInteger)itemIndexForIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *item = [self itemAtIndexPath:indexPath];

    NSInteger index = [self.workouts indexOfObjectIdenticalTo:item];

    return index;
}

#pragma mark - table view datasource

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self numberOfItemsInDays:self.days[section]];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"workoutrow";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSDictionary *workouts = [self itemAtIndexPath:indexPath];

    cell.textLabel.text = workouts[@"name"];
    cell.detailTextLabel.text = [_reps objectAtIndex:indexPath.row];
    //    NSLog(@"cell.textLabel.text %@", cell.textLabel.text);


    [cell layoutIfNeeded];
    return cell;

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





@end

这是我的应用模拟器运行结果。 [![模拟器结果] [1]] [1]

http://i.stack.imgur.com/VQWzO.png

你可以看到“DEAD LIFT”的字幕标签“15,12,10,8,6”由于某些我无法找到的原因而重复。

根据我设置的数组“reps”,它应该不是那样的。

顺便说一下,这个tableview是从另一个tableview单元格中进行的,这是我代码中的侧面菜单栏tableview单元格。

抱歉由于声誉而无法上传图片

提前谢谢

1 个答案:

答案 0 :(得分:0)

每次开始新的一天时,切换部分时会重置indexPath.row,导致它从reps数组的开头开始打印。