分享一下今天郁闷了几个小时的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方法只被调用了一次。
小弟乃新手,对此理解并不是很深,希望懂的大侠详细解释一下。也欢迎各位讨论,共同进步~~~