继承UITableViewController出现的界面紊乱问题

      分享一下今天郁闷了几个小时的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方法只被调用了一次。

     小弟乃新手,对此理解并不是很深,希望懂的大侠详细解释一下。也欢迎各位讨论,共同进步~~~


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值