简体   繁体   中英

Can't get my TableView to work

'Can't get this to work at all ! I had a nice simple class for a TableView that took up the entire screen, but decided i'd slice it in half, so the table only took the top half of the screen. This is my code:

This is the TableSplitViewController.h

 @interface TableSplitViewController : UIViewController
{
    ChildrenTableViewController *firstController;
    IBOutlet UITableView *firstTable;

}

This is the TableSplitViewController.m:

- (void)viewDidLoad
  { 
[super viewDidLoad];
if (firstController == nil) {
    firstController = [[ChildrenTableViewController alloc] init];
}
    [firstTable setDataSource:firstController];
    [firstTable setDelegate:firstController];
    firstController.view = firstController.tableView;

 }

This is the ChildrenTableViewController.h:

@interface ChildrenTableViewController : UITableViewController

@property (nonatomic, strong) NSMutableArray *childname;

@end

This is the ChildrenTableViewController.m:

    @implementation ChildrenTableViewController

         @synthesize childname;



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

  - (void)viewDidLoad
    {
     [super viewDidLoad];
     self.childname = [[NSMutableArray alloc]
                      initWithObjects:@"Oliver",
                      @"Stuart",
                      @"Ross",
                      @"Alex", nil];
}

   -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
   {
      if ([[segue identifier] isEqualToString:@"ShowChildrenDetails"]) {
            ChildrenDetailViewController *detailViewController = [segue     destinationViewController];

          NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];

          detailViewController.childrenDetailModel = [[NSArray alloc]
                                                    initWithObjects: [self.childname  objectAtIndex:[myIndexPath row]], nil];
     }
  }


    - (void)didReceiveMemoryWarning
  {
     [super didReceiveMemoryWarning];
     // Dispose of any resources that can be recreated.
  }



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

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
   {

      // Return the number of rows in the section.
      return [self.childname count];
  }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
   {
        static NSString *cellIdentifier = @"childrenTablecell";

      ChildrenTableViewCell * cell = [tableView   dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
          cell = [[ChildrenTableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
       }
      // Configure the cell..
      cell.childname.text = [self.childname objectAtIndex:[indexPath row]];


    return cell;
}

This is ChildrenTableViewCell.h:

@interface ChildrenTableViewCell : UITableViewCell
@property (nonatomic, strong) IBOutlet UILabel *childname;

@end

This is ChildrenTableViewCell.m:

@implementation ChildrenTableViewCell

@synthesize childname;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

This is a print screen of the storyboard layout. I've attached the label with the childname so it should print the name out :

屏幕截图1

This is a print screen of it running, its weird it has the 4 rows, i can select them, but the labels are not being printed :

![Screenshot 2][2]

Fully appreciate all the help and support you can provide. Thanks :)

[2] http://puu.sh/1USzX/512ede1328

Thanks to IOSDEV i managed to find out why it was doing it.

cell.childname.text = [self.childname objectAtIndex:[indexPath row]];
NSLog(@"%@",[self.childname objectAtIndex:[indexPath row]]);
NSLog(@"%@",cell.childname.text);

This is what i put into my code.

and this is what i got:

2013-01-30 15:33:52.465 TableViewStory[17509:907] Oliver
2013-01-30 15:33:52.467 TableViewStory[17509:907] (null)
2013-01-30 15:33:52.470 TableViewStory[17509:907] Stuart
2013-01-30 15:33:52.472 TableViewStory[17509:907] (null)
2013-01-30 15:33:52.473 TableViewStory[17509:907] Ross
2013-01-30 15:33:52.474 TableViewStory[17509:907] (null)
2013-01-30 15:33:52.476 TableViewStory[17509:907] Alex
2013-01-30 15:33:52.477 TableViewStory[17509:907] (null)

So as you can see cell.childname.text is equal to null, this is the reason why the text isn't coming up :PI don't understand why it is though.

You should use the initWithStyle in the TableSplitViewController.m

firstController = [[ChildrenTableViewController alloc] initWithStyle:UITableViewStyleGrouped]; 

For some reason when I didn't use it to init the tableView I had some similar problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM