Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set UITableView Delegate and DataSource

This is my problem: I have this small UITableView in my storyboard:enter image description here

And this is my code:

SmallTableViewController.h

#import <UIKit/UIKit.h>
#import "SmallTable.h"

@interface SmallViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITableView *myTable;

@end

SmallTableViewController.m

#import "SmallViewController.h"

@interface SmallViewController ()

@end

@implementation SmallViewController
@synthesize myTable = _myTable;

- (void)viewDidLoad
{
    SmallTable *myTableDelegate = [[SmallTable alloc] init];
    [super viewDidLoad];
    [self.myTable setDelegate:myTableDelegate];
    [self.myTable setDataSource:myTableDelegate];

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

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

@end

Now as you can see, I want to set an instance called myTableDelegate as Delegate and DataSource of myTable.

This is the Source of SmallTable class.

SmallTable.h

#import <Foundation/Foundation.h>

@interface SmallTable : NSObject <UITableViewDelegate , UITableViewDataSource>

@end

SmallTable.m

@implementation SmallTable

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

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

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

    // Configure the cell...
    cell.textLabel.text = @"Hello there!";

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Row pressed!!");
}

@end

I implemented all the UITableViewDelegate and UITableViewDataSource method that the app need. Why it just crash before the view appear??

Thanks!!

like image 294
Marco Manzoni Avatar asked Jun 29 '12 15:06

Marco Manzoni


People also ask

What is a uitableviewdatasource/delegate?

What is a UITableViewDatasource/Delegate? The UITableViewDatasource has a few methods that help you manage data and provide the cells for your table view. The general methods that gets used from the datasource in most tutorials would include the numberOfRowsInSection and the cellForRowAt indexPath.

Are Table View data source and delegate methods too easy to write?

The table view data source and delegate methods are very easy to write. How much values does it really add if you spend a lot of time and effort to come up with a generic data source so that you can reuse it in another UITableViewController subclass?

What is the nstableviewdatasource protocol?

UITableview's Data Source Methods : The NSTableViewDataSource protocol declares the methods that an instance of NSTableView that provides the data to a table view and allows editing of the contents of its data source object. All the methods are described in the following.

What methods are required from the data source in a Tableview?

The following methods are required from the data source: tableView:numberOfRowsInSection: and tableView:cellForRowAtIndexPath:. If your table is a grouped table, you must also implement numberOfSectionsInTableView:.


2 Answers

rickster is right. But I guess you need to use a strong qualifier for your property since at the end of your viewDidLoad method the object will be deallocated anyway.

@property (strong,nonatomic) SmallTable *delegate;

// inside viewDidload

[super viewDidLoad];
self.delegate = [[SmallTable alloc] init];    
[self.myTable setDelegate:myTableDelegate];
[self.myTable setDataSource:myTableDelegate];

But is there any reason to use a separated object (data source and delegate) for your table? Why don't you set SmallViewController as both the source and the delegate for your table?

In addition you are not creating the cell in the correct way. These lines do nothing:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell...
cell.textLabel.text = @"Hello there!";

dequeueReusableCellWithIdentifier simply retrieves from the table "cache" a cell that has already created and that can be reused (this to avoid memory consumption) but you haven't created any.

Where are you doing alloc-init? Do this instead:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell) {
    cell = // alloc-init here
}
// Configure the cell...
cell.textLabel.text = @"Hello there!";

Furthermore say to numberOfSectionsInTableView to return 1 instead of 0:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}
like image 115
Lorenzo B Avatar answered Oct 20 '22 14:10

Lorenzo B


Presumably you're using ARC? Your myTableDelegate is only referenced in a local variable in viewDidLoad -- once that method ends, it's deallocated. (In the delegate/datasource pattern, objects do not own their delegates, so the table view's references back to your object are weak.) I wouldn't expect that alone to cause a crash, but it's likely key to your problem.

like image 23
rickster Avatar answered Oct 20 '22 16:10

rickster