【问题标题】:Master Detail Storyboard: Table View not showing cells (Objective-C)主从情节提要:表格视图不显示单元格(Objective-C)
【发布时间】:2012-08-22 15:55:55
【问题描述】:

我在主视图控制器没有显示任何单元格时遇到问题。情况是这样的:

该应用使用情节提要。

当应用启动时,它会转到导航控制器

一个按钮被按下并连接到表 ViewController,它被设置为“推送”到它。

我已经添加了对象并制作了一个单元格/详细信息视图或其他任何内容。

由于某种原因,单元格不会出现!!!

这是文件:

MasterViewController.h:

#import <UIKit/UIKit.h>
#import "CraftingDetail.h"
#import "Crafting.h"

@class CraftingList;

@interface CraftingMaster : UITableViewController

@property (strong, nonatomic) CraftingDetail *detailViewController;
@property (strong, nonatomic) CraftingList *CL;

@end

MasterViewController.m:

#import "CraftingMaster.h"
#import "CraftingList.h"

@interface CraftingMaster ()

@end

@implementation CraftingMaster

@synthesize detailViewController = _detailViewController;
@synthesize CL;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
        self.CL = [[CraftingList alloc] init];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for     this view controller.
     self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return  self.CL.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.CL.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...

    cell.textLabel.text = [self.CL craftingAtIndex:indexPath.row].Title;

    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath   *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:      (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
       // Delete the row from the data source
       [tableView deleteRowsAtIndexPaths:@[indexPath]     withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
       // Create a new instance of the appropriate class, insert it into the array, and  add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath   *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
     // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc]   initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end

DetailViewController.h:

#import <UIKit/UIKit.h>

@interface CraftingDetail : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *Image;
@property (strong, nonatomic) IBOutlet UITextView *Description;

@end

【问题讨论】:

    标签: objective-c storyboard


    【解决方案1】:

    这是一个老问题,但在开始使用表视图进行开发时,表视图意外为空是一个常见问题,因此希望这个答案对某人有用。

    当您的表格视图没有单元格时,您需要检查以下几点:

    • 您的数据源对象(本例中为self.CL)是否有效? (即,它们是 != nil 并指向正确的对象吗?)
    • numberOfSectionsInTableView: 是否返回大于零的整数值?
    • tableView:numberOfRowsInSection: 是否返回大于零的整数值?

    以下是上面MasterViewController.m中需要注意的几个问题:

    • InitWithStyle: 在情节提要中实例化视图控制器时不会执行。相反,应该使用initWithCoder:。我怀疑这是 JomanJi 痛苦的根源,因为这导致 self.CL 没有被实例化。 (顺便说一句,数据源对象/属性:CL 应该通过将值直接分配给 _CL ivar 而不是属性来实例化。请参阅“Initializing a property, dot notation”了解原因)。
    • 由于numberOfSectionsInTableView:tableView:numberOfRowsInSection: (即“return self.CL.count;”)返回(很可能)相同的值,因此表格视图将显示与每个部分中的单元格相同数量的部分每个部分的单元格包含与其他部分相同的数据。我怀疑这种效果是开发人员想要的。 (这当然是除非CraftingList 中的count 访问器方法确实很奇怪)。

    如果没有看到CraftingList 的代码,就不可能确切地确定问题所在。但是,考虑到问题的年龄,我怀疑 JomanJi 已经自己解决了。

    【讨论】:

    • 嘿,thanx 的回答,我已经不再研究这个了,但我可能会考虑以后使用,再次感谢
    • 没问题。没想到您是,但认为其他人可能有兴趣知道为什么他们在表视图中看不到任何内容。 (我知道当它发生在我身上时我做到了!)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2014-04-01
    相关资源
    最近更新 更多