的代碼崩潰低於: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委託和調用時隙數..
這隻會讓問題變得更糟。 '[NSArray arrayWithObjects:...]'返回的數組不屬於你,所以自動釋放它會導致它最終被釋放太多次。 – Anomie 2011-03-13 05:27:16
@Anomie我非常瞭解底層流程。你不需要教我。關於「它會導致它最終被釋放太多次」真的很有趣。 – bioffe 2011-03-13 05:30:24
那你爲什麼發表這樣一個公然錯誤的評論? – Anomie 2011-03-13 05:34:41