关于UITableViewController导致程序退出

iOS 码拜 9年前 (2015-05-07) 534次浏览 0个评论
 

我写了一个继承自UITableViewController的子类,其中一个函数会导致程序一启动就挂掉。我把那个函数给注释掉程序就不会出错了,那个函数是:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [[masterColorList objectAtIndex:section] count];
}
20分
[masterColorList objectAtIndex:section]
section 应该超过了 masterColorList 的 count,
做个测试。
还有个函数,numberOfSections 你返回的是多少。
15分
先问楼主怎么得到的section的多少的????把这个问题解决了后面的就好办了,还有一点,在加载TableView的内容时你的masterColorList不因该有其他操作!
15分
[masterColorList objectAtIndex:section]  要打印出来看看
只要是我把- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section这个函数注释掉程序就不会出错了,但是也就没有数据显示了。
我把代码贴出来,大侠们帮看看啊,刚刚开始自学,啥都不会啊,多谢了。

头文件

#import <Foundation/Foundation.h>


@interface RootViewController : UITableViewController
{
	NSArray *masterColorList;
}

@property(retain)NSArray *masterColorList;
@end

代码文件:

#import "RootViewController.h"
#import "CoreGraphics/CGGeometry.h"


@implementation RootViewController
@synthesize masterColorList;

- (id)initWithCoder:(NSCoder *)decoder
{
	if (self = [super initWithCoder:decoder])
	{

		NSArray *colorList = [NSArray arrayWithObjects:
							  [NSDictionary dictionaryWithObjectsAndKeys:@"brownColor",@"titleValue",[UIColor brownColor],@"colorValue",nil],
							  [NSDictionary dictionaryWithObjectsAndKeys:@"orangeColor",@"titleValue",[UIColor orangeColor],@"colorValue",nil],
							  [NSDictionary dictionaryWithObjectsAndKeys:@"purpleColor",@"titleValue",[UIColor purpleColor],@"colorValue",nil],
							  [NSDictionary dictionaryWithObjectsAndKeys:@"redColor", @"titleValue",[UIColor redColor],@"colorValue",nil],
							  nil];


		NSArray *otherColorList = [NSArray arrayWithObjects:
								   [NSDictionary dictionaryWithObjectsAndKeys:@"RGB:0.25/0/0", @"@titleValue",[UIColor colorWithRed:0.25 green:0 blue:0 alpha:1],@"colorValue",nil],
								   [NSDictionary dictionaryWithObjectsAndKeys:@"RGB:0.5/0/0", @"@titleValue",[UIColor colorWithRed:0.5 green:0 blue:0 alpha:1],@"colorValue",nil],
								   [NSDictionary dictionaryWithObjectsAndKeys:@"RGB:0.75/0/0", @"@titleValue",[UIColor colorWithRed:0.75 green:0 blue:0 alpha:1],@"colorValue",nil],
								   nil
								   ];

		masterColorList = [NSArray arrayWithObjects:colorList,otherColorList,nil];
		[masterColorList retain];
	}

	return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return masterColorList.count;
}

/*
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
	 return [[masterColorList objectAtIndex:section] count];
 }
*/



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *MyIdentifier = @"MyIdentifier";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

	if (cell = nil)
	{
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
	}

	cell.textColor = [[[masterColorList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"colorValue"];
	cell.selectedTextColor = [[[masterColorList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"colorValue"];
	cell.text = [[[masterColorList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"titleValue"];

	return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	if (section == 0)
	{
		return @"SDK Colors";
	}
	else if (section == 1)
	{
		return @"RGB Colors";
	}
	return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
}

- (void)dealloc
{
	[super dealloc];
	[masterColorList release];
}

- (void)viewDidLoad
{
	[super viewDidLoad];
}


- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
	[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
}

- (void)viewDidDisappear:(BOOL)animated
{
}

- (void)didReceiveMemoryWarning
{
	[super didReceiveMemoryWarning];
}
@end
回1楼,section应该没超啊,没法测试,只要是有那个函数,即便返回一个手工赋值的变量或者函数为空,也会导致程序挂掉。别:numberOfSections返回的是2。

回2楼,我把代码贴出来了,麻烦帮看下啊,我也不知道section怎么得到的。

回3楼,没法测试啊,那个函数在就会导致程序挂掉


– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
中,
if (cell == nil)
少了一个等号

建议以后写判断语句时,写成类似这样
if ( nil == cell)


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于UITableViewController导致程序退出
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!