学习表视图(Table View)的应用时,自己写了个demo,最后表格出来了,可是滑动时报错了,报错如下:
这是我ViewController.m部分的代码:
#import "ViewController.h" @interface ViewController () @end @implementation ViewController
{
NSArray *tableData;
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tableData = [NSArray arrayWithObjects:@"Egg Benedict" , @"Mushroom Risotto" , @"Full Breakfast" , @"Hamburger" ,@"Ham and Egg Sandwich" , @"Creme brelee" , @"white chocolate donut" , @"starbucks coffee" , @"vegetable curry" , @"instant noodle with egg" , @"noodle with bbq pork" , @"japanese noodle" , @"green tea" , @"thai shrimp cake" , @"angry birds cake" , @"ham and cheese panini" , nil];
//[tableData retain]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
} //[[cell textLabel] setText:[tableData objectAtIndex:[indexPath row]]];
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"icon.png"]; return cell; } - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
经过反复的测试后,解决办法如下:
在第17行加上:
[tableData retain];
这样就可以解决报错问题了。