分享一下今天鬱悶了幾個小時的Bug,以後寫代碼的時候多多注意啊。這樣的錯誤,ca,很不好惹。
這個Demo全是用代碼寫的,建的是一個SinglView Application。用的是Xcode4.3.1,建立一個ViewController,直接繼承自UITableViewController。
.h檔案如下:
#import <UIKit/UIKit.h>@interface rootSettingViewController: UITableViewController{ NSArray *infoArray; NSArray *settingArray; NSArray *sectionArray; UITableView *settingTableView;}@end
.m檔案如下:
#import "rootSettingViewController.h"@interface rootSettingViewController ()@end@implementation rootSettingViewController- (void)viewDidLoad{ [super viewDidLoad]; settingArray=[[NSArray alloc] initWithObjects:@"顯示效果",@"常規",@"其他", nil]; infoArray=[[NSArray alloc] initWithObjects:@"個人資訊", nil]; sectionArray=[[NSArray alloc] initWithObjects:@"個人資訊",@"個性定製",nil]; settingTableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420) style:UITableViewStyleGrouped]; [settingTableView setDataSource:self]; [settingTableView setDelegate:self]; [self.view addSubview:settingTableView]; // self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"返回" style:107 target:self action:@selector(backTo:)]; // self.title=@"設定";//返回按鈕,與這篇文章要說的問題無關,可無視。}-(void)backTo:(id)sender{ [self.navigationController popViewControllerAnimated:YES];}- (void)viewDidUnload{ [super viewDidUnload]; }- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return (interfaceOrientation == UIInterfaceOrientationPortrait);}#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ // Return the number of sections.// NSLog(@"====%d",[sectionArray count]); return [sectionArray count];}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ // Return the number of rows in the section. NSInteger rows; switch (section) { case 0: rows=[infoArray count]; break; case 1: rows=[settingArray count]; break; default: break; } return rows;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSInteger section=indexPath.section; NSInteger row=indexPath.row; static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell==nil) { cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.imageView.image=[UIImage imageNamed:@"home_system.png"]; switch (section) { case 0: cell.textLabel.text=[infoArray objectAtIndex:row]; NSLog(@"cell.textLabel.text==%@",cell.textLabel.text); break; case 1: cell.textLabel.text=[settingArray objectAtIndex:row]; NSLog(@"cell.textLabel.text=%@",cell.textLabel.text); break; default: break; } cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; cell.selectionStyle=UITableViewCellSelectionStyleGray; return cell;}
整體代碼看上去,沒什麼問題,一運行就傻了。結果見:
我開始寫的時候想偷懶,直接繼承UITableViewController,這樣在工程建立的時候就會自動產生UITableViewController 裡的delegate方法。圖的是方便,誰曾想,如此悲劇。通過
NSLog(@"cell.textLabel.text==%@",cell.textLabel.text);
輸出的結果可以看到,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
這些delegate方法被執行多次
調了好久,最後去掉UITableViewController,繼承自UIViewController,然後添加UITableViewDelegate,UITableViewDataSource.修改後.h檔案如下:
#import <UIKit/UIKit.h>@interface rootSettingViewController: UIViewController<UITableViewDelegate,UITableViewDataSource>{ NSArray *infoArray; NSArray *settingArray; NSArray *sectionArray; UITableView *settingTableView;}@end
這下就正常了。
通過NSLog輸出,可以看到,裡面的delegate方法只被調用了一次。
小弟乃新手,對此理解並不是很深,希望懂的大俠詳細解釋一下。也歡迎各位討論,共同進步~~~