执行委托时UITableView崩溃

时间:2011-03-13 05:20:46

标签: iphone objective-c 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委托和调用时间段计数..

2 个答案:

答案 0 :(得分: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,直到消息范围结束。技术上自动释放池业务是异步,因此它可能会崩溃或者可能不会崩溃,但是您应该假设此对象在此时是僵尸。因此,您必须使用-retain以保留此对象以供其他方法使用。如果您明确使用-retain,那么在完成此对象后必须明确使用-release。那么为什么不马上使用[[NSArray alloc] initWithObjects:...]。它在这里更合适。您也可以在此处使用保留属性,但是如果没有内部变量,则不希望公开内部变量。

答案 1 :(得分:0)

viewDidLoad中,您将新创建的(和自动释放的)NSArray分配给ivar而不是属性,这意味着它为而不是保留。因此,当它到达tableView:numberOfRowsInSection:时,数组已被处理掉,并且您得到EXC_BAD_ACCESS或类似的崩溃。

出于这个原因,我通常将我的ivars命名为_name而不是name来匹配该属性。这确实意味着合成行必须是更复杂的@synthesize name=_name,但它会防止意外地分配给ivar而不是属性。