UITableViewCell不显示数组

时间:2015-07-19 05:38:47

标签: ios objective-c xcode uitableview segue

我正在使用带有SWRevealView控制器的Fitness(锻炼)调度程序应用程序,它提供侧栏菜单功能。

我试图将侧栏菜单中的一个单元格链接到另一个Tableview控制器,我在其中设置了segue。

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中存在错误。

1 个答案:

答案 0 :(得分:0)

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