0

好吧,我知道我的 n00b-ness 会惹恼这里的某个人,所以认为这是一个公平的警告。我对 Obj-C 来说是新鲜的,所以对你来说很明显的东西对我来说很可能不会。

我一直在关注TableViewControllers 上的本教程,但我一辈子都无法让单元格标题出现。我已经将网站上的每一行代码都删掉了,并调试了一个 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 行代码,只删掉了似乎与视图控制器本身有关的部分。有些事情告诉我,我只是省略了必要的连接代码行,但是我对语言的完整介绍状态以及缺少调试器错误并没有让我感到困惑。有任何想法吗?任何帮助(不包括挑剔)都值得赞赏和欢迎。

4

2 回答 2

1

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

  1. 在你的 cellFowAtIndexPaht 方法中

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

于 2013-08-31T18:07:40.580 回答
0

我能够重现您的问题,如下所示:

您当前tableView:cellForRowAtIndexPath:的实现要求单元格有UILabel一个标签为 100 的子视图。

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

单元格中的标签需要配置这个标签...

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

选项1

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

在此处输入图像描述

选项 2

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

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

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

于 2013-08-31T18:35:51.837 回答