views:

335

answers:

3

Hello,

I'm new to iPhone application development. I'm trying to understand how to use UITableView.

I'm wrote simple code:

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    }
 cell.textLabel.text = @"Hello";
    return cell;
}

UITable shows content, but if i'm drag table contents my application terminates. You can see video: http://www.youtube.com/watch?v=TucTVJVhSD0

I tried to everything with array:

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

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

 return [hello count] ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    }
 cell.textLabel.text = [hello objectAtIndex:indexPath.row];
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 [tableView deselectRowAtIndexPath:indexPath animated:NO];
 NSLog(@"Selected") ;
}



- (void) awakeFromNib
{
 hello = [[NSArray alloc] initWithObjects:@"hello", @"world", @"end", nil];
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}

Content is shown and if I'm selecting item I'm getting:

[Session started at 2010-03-16 19:21:48 +0200.]
2010-03-16 19:21:52.295 ViewTest[1775:207] *** -[ViewTestViewController respondsToSelector:]: message sent to deallocated instance 0x3911ec0

I'm total new to iPhone programming. And as I see everything I do - I'm just getting application terminated ..

+1  A: 

Your table view code looks fine. Did you implement any other delegate methods in the ViewTestViewController?

Try running the application in the debugger. When it crashes, look at the stack trace. It should give a better hint.

St3fan
No, i'm not implementing any protocols. I just set dataSource to my ViewTestViewController in IB. #import <UIKit/UIKit.h>@interface ViewTestViewController : UIViewController{ NSArray *hello ; }@end
kesrut
A: 

(1) I think you may have released your tableview controller instance and killed it while it was still in use so that when the tableview sends it one of its delegate or datasource messages it crashes.

(2) Alternatively, the error message says that you are attempting to send the respondsToSelector: message to an instance of ViewTestViewController when you should be sending it to the class. (-[ViewTestViewController respondsToSelector:] vs + [ViewTestViewController respondsToSelector:] the "-" and "+" make all the difference.)

So, somewhere in your code, probably your app delegate, you've got code that says:

ViewTestViewController *myTableViewController= //..however you setup the controller
[myTableViewController respondsToSelector:@selector(someMethod)];

...when you should have...

[ViewTestViewController respondsToSelector:@selector(someMethod)];

I think (1) the most likely.

TechZen
A: 

Thanks to all. I found problem. I found i haven't connected viewController IBOutlet with App Delegate. But why View was shown ?

@interface ViewTestAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    ViewTestViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet ViewTestViewController *viewController;
kesrut