2015-07-19 129 views
-1

我正在使用带有SWRevealView控制器的Fitness(锻炼)调度程序应用程序,该应用程序提供侧栏菜单功能。UITableViewCell不显示阵列中的内容

我试图将一个单元格从侧栏菜单链接到另一个Tableview控制器,在那里我设置了它们之间的连线。

segue很好,但它不显示我在可变数组中的文本数据。

MainViewController.h

#import <UIKit/UIKit.h> 

@interface MainViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 

@property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton; 
@end 

MainViewController.m

#import "MainViewController.h" 
#import "SWRevealViewController.h" 

@interface MainViewController() 

@property (nonatomic) NSMutableArray *plans; 
@property (nonatomic) NSArray *categories; 

@end 

@implementation MainViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
// [UITableView setDelegate:self]; 
// [UITableView setDataSource:self]; 

    self.plans = @[@{@"name" : @"Week 1-3", @"category" : @"Buff Dudes Workout"}, @{@"name" : @"Week 4-6", @"category" : @"Buff Dudes Workout"}, @{@"name" : @"Week 7-9", @"category" : @"Buff Dudes Workout"}, @{@"name" : @"Week 10-12", @"category" : @"Buff Dudes Workout"}].mutableCopy; 
    self.categories = @[@"Buff Dudes Workout"]; 


    self.title = @"Workout Programs"; 

    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 *) itemsInCategory:(NSString *)targetCategory { 
    NSPredicate *matchingPredicate = [NSPredicate predicateWithFormat:@"category == %@", targetCategory]; 
    NSArray *categoryItems = [self.plans filteredArrayUsingPredicate:matchingPredicate]; 

    return categoryItems; 
} 

- (NSInteger)numberOfItemsInCategory:(NSString *)targetCategory { 
    return [self itemsInCategory:targetCategory].count; 
} 

- (NSDictionary *)itemAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *category = self.categories[indexPath.section]; 
    NSArray *categoryItems = [self itemsInCategory:category]; 
    NSDictionary *item = categoryItems[indexPath.row]; 

    return item; 
} 

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

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

    return index; 
} 

//- (void)removeitemsAtindexPath:(NSIndexPath *)indexPath { 
// NSInteger index = [self itemIndexForIndexPath:indexPath]; 
// [self.items removeObjectAtIndex:index]; 


#pragma mark - table view datasource 

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

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

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

    NSDictionary *plans = [self itemAtIndexPath:indexPath]; 

    cell.textLabel.text = plans[@"name"]; 



    return cell; 

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

@end 

我觉得有什么不对,我ellForRowAtIndexPath method

+0

检查你的nsmutable在使用前是否正确分配了 – Lorenzo

+0

为什么'setDelegate'和'setDataSource'被注释掉? – ozgur

+0

尝试'NSLog(@“cell.textLabel.text%@”,cell.textLabel.text);'设置文本后'cell.textLabel.text = plans [@“name”];'。它实际上有价值吗?或者它是空的? – SFF

回答

0
  1. 确保您的tableView dataSource和IB在IB委托连接到文件所有者(MainViewController)。
  2. 设置计划和类别后,添加对tableView的引用并调用tableView.reloadData()