繼承UITableViewController出現的介面紊亂問題

來源:互聯網
上載者:User

      分享一下今天鬱悶了幾個小時的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方法只被調用了一次。

     小弟乃新手,對此理解並不是很深,希望懂的大俠詳細解釋一下。也歡迎各位討論,共同進步~~~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.