2011-03-13 61 views
0

的代码崩溃低于:UITableView的执行委托时

@interface PreferenceViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> { 
    UITableView * preference; 
    UISegmentedControl * dimension; 
    NSArray * timeslot; 
} 

@property (nonatomic, retain) IBOutlet UITableView * preference; 
@property (nonatomic, retain) IBOutlet UISegmentedControl * dimension; 
@property (nonatomic, retain) NSArray * timeslot; 

@end 

- (void)viewDidLoad 
{ 
    preference.delegate = self; 
    preference.dataSource = self; 
    timeslot = [NSArray arrayWithObjects:@"7:00-8:00", @"8:00-9:00", @"9:00-10:00", @"10:00-11:00", @"11:00-12:00", @"12:00-13:00", @"13:00-14:00", @"14:00-15:00", @"15:00-16:00", @"16:00-17:00", nil]; 
    NSLog(@"size of array is %d", [timeslot count]); 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
} 

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - Table View delegate 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (dimension.selectedSegmentIndex == 0){ 
     return [timeslot count]; 
    }else 
     return 15; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    //NSLog(@"TESTING"); 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    // this is the MAGIC line! 

    if (dimension.selectedSegmentIndex == 0) 
     cell.text = [timeslot objectAtIndex:indexPath.row]; 

    return cell; 
} 

想知道是什么引起的崩溃?它发生在numberOfRowsInSection委托和调用时隙数..

回答

1
timeslot = [NSArray arrayWithObjects:@"7:00-8:00", @"8:00-9:00", @"9:00-10:00", @"10:00-11:00", @"11:00-12:00", @"12:00-13:00", @"13:00-14:00", @"14:00-15:00", @"15:00-16:00", @"16:00-17:00", nil]; 

这条线应该有-retain

timeslot = [[NSArray arrayWithObjects:@"7:00-8:00", @"8:00-9:00", @"9:00-10:00", @"10:00-11:00", @"11:00-12:00", @"12:00-13:00", @"13:00-14:00", @"14:00-15:00", @"15:00-16:00", @"16:00-17:00", nil] retain]; 

UPD -autorelease不是一个好主意。您只能自动释放/释放您拥有的对象。当您使用-arrayWithObjects时,您的对象由本地AutoreleasePool拥有。

这是简单的说法。每当你创建一个对象而没有-alloc-copy方法它是自动释放,这意味着它将有引用计数1,直到消息范围的结束。技术上autorelease池业务是异步,所以它可能会崩溃或可能不会,但你应该认为这个对象是僵尸在这一点上。所以你必须使用-retain为了保存这个对象用于其他方法的使用。如果您明确使用-retain,则必须在完成此对象时明确使用-release。那么为什么不马上使用[[NSArray alloc] initWithObjects:...]。这里更合适。你也可以在这里使用保留属性,但是当你没有时,你不想暴露内部变量。

+1

这只会让问题变得更糟。 '[NSArray arrayWithObjects:...]'返回的数组不属于你,所以自动释放它会导致它最终被释放太多次。 – Anomie 2011-03-13 05:27:16

+0

@Anomie我非常了解底层流程。你不需要教我。关于“它会导致它最终被释放太多次”真的很有趣。 – bioffe 2011-03-13 05:30:24

+0

那你为什么发表这样一个公然错误的评论? – Anomie 2011-03-13 05:34:41

0

viewDidLoad,要指定新创建(和自动释放)的NSArray的伊娃,而不是财产,这意味着它是保留你。因此,当它到达tableView:numberOfRowsInSection:时,该数组已被处理掉,并且您得到EXC_BAD_ACCESS或类似的崩溃。

因此,我通常将我的ivars _name而不是name命名为匹配属性。这确实意味着合成线必须是更复杂的@synthesize name=_name,但它可以防止意外分配给伊娃而不是属性。

+0

你说我应该改变@synthesize时隙到@synthesize timeslot = _timeslot; ?因为我也尝试过,它不起作用 – aherlambang 2011-03-13 05:31:48

+0

@EquinoX不听他的。您需要在这里留存财产。只要将'arrayWithObjects'更改为'initWithObjects',并在完成之后释放分配的对象。 – bioffe 2011-03-13 05:33:46

+0

@bioffe感谢兄弟..但心灵解释与arrayWithObjects不起作用? – aherlambang 2011-03-13 05:36:54