TableViewController单元格未填充数据

时间:2013-08-31 17:53:15

标签: ios uitableview nsarray

好吧,我知道我会用我的n00b-ness来惹恼别人,所以请认为这是一个公平的警告。我对Obj-C来说是一条新鲜的鱼,所以对我来说最明显的可能是对我不利。

我一直在关注TableViewControllers上的this tutorial,而且我不能在我的生活中获得单元格标题。我已对网站上的每一行代码进行剪切和零售,并调试了SIGABRT错误,但即使现在数据也没有出现。

以下是MasterViewController.h和/.m文件的内容:

标题文件:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import <UIKit/UIKit.h>

@class DetailViewController;

@interface MasterViewController : UITableViewController <NSFetchedResultsControllerDelegate, UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) DetailViewController *detailViewController;

//  Create property "equations" as an instance of NSArray:
@property (strong, nonatomic) NSMutableArray *equations;

@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

@end

实施档案:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "MasterViewController.h"
#import "DetailViewController.h"

@interface MasterViewController ()
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end

@implementation MasterViewController

//  Synthesize NSArray instance for equation storage:
@synthesize equations = _equations;


//  Segue linking as per DetailViewController.h/.m:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showDetail"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    DetailViewController *destViewController = segue.destinationViewController;
    destViewController.equationName = [_equations objectAtIndex:indexPath.row];
}
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]     initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
self.title = @"Equations";
if (!_equations)
{
    _equations = [[NSMutableArray alloc] initWithObjects:
                  // Atomic structure equations:
                  @"Energy-Frequency Relation",
                  @"Energy-Frequency-Wavelength Relation",
                  @"Energy-Quantum Number Relation",
                  @"Momentum-Mass-Frequency",
                  @"Speed of Light Definition",
                  // Equilibrium equations:
                  @"Equilibrium Acid Constant",
                  @"Equilibrium Base Constant",
                  @"Water Equilibrium Constant",
                  @"pH Calculation",
                  @"pH-Acid Constant Relation",
                  @"pOH-Base Constant Relation",
                  @"pKa Derivation",
                  @"pKb Derivation",
                  @"pOH Calculation",
                  @"Gas-pressure Equlibrium",
                  // Gas/solution chemistry equations:
                  @"Ideal-Gas Law",
                  @"Partial-pressure equation",
                  @"Total pressure (3 partials)",
                  @"mol-Molarity Calculation",
                  @"Kelvin-Celsius Relation",
                  @"Fahrenheit-Celsius Relation",
                  @"Density Calculation",
                  @"Kinetic Energy per Molecule",
                  @"Kinetic Energy per Mol",
                  @"Molarity Equation",
                  @"Molality Equation",
                  @"Absorbance Equation",
                  @"Freezing Point Depression",
                  @"Boiling Point Elevation"
                  // Redox Equations:
                  @"Electrical current definition",
                  @"Equilibrium vs. Reduction Potential",
                  // Thermochemical relations:
                  @"Change in Free Energy",
                  @"Molar Heat Capacity",
                  @"Frequency to Rate Factor",
                  nil];
    }
}
#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //  Return call from NSArray *equations as to the count of elements in the table view:
    return [_equations count];
}

- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
UILabel *lblName = (UILabel *)[cell viewWithTag:100];
[lblName setText:[_equations objectAtIndex:[indexPath row]]];
return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return NO;
}

我放弃了所有250行代码,只删除了与视图控制器本身相关的代码。有些东西告诉我,我只是遗漏了必要的连接代码,但是我对语言的完整介绍状态以及缺少调试器错误并没有让我感到厌烦。有任何想法吗?任何帮助(挑剔排除)都不仅值得赞赏和欢迎。

2 个答案:

答案 0 :(得分:1)

@“Cell”可能是你可以在这里查看的几件事。

    您的cellFoRowAtIndexPaht方法

    中的
  1. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    
    //add this
    if (cell == nil)
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease];
    
  2. 仔细检查您的表数据源和表委托是否正确连接到表视图。我认为当你从故事板创建uitableviewcontroller时,它应该自动为你连接,但不会对双重检查造成伤害。

答案 1 :(得分:0)

我能够按如下方式重现您的问题:

您当前的tableView:cellForRowAtIndexPath:实施要求该单元格具有标记为100的UILabel子视图。

UILabel *lblName = (UILabel *)[cell viewWithTag:100];

单元格中的标签需要使用此标记配置...

如果在Interface Builder中使用“基本”单元格类型,则有两个选项:

选项1

您必须选择标题标签并将其标记设置为100。

enter image description here

选项2

更改方法以直接访问文本标签。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    [cell.textLabel setText:_equations[indexPath.row]];
    return cell;
}

对于您提供自己标签的自定义单元格类型,您也可以使用与选项2 类似的方法(如果不相同)。