Table View滑动时报错

时间:2022-04-22
本文章向大家介绍Table View滑动时报错,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

学习表视图(Table View)的应用时,自己写了个demo,最后表格出来了,可是滑动时报错了,报错如下:

这是我ViewController.m部分的代码:

 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()
 4 
 5 @end
 6 
 7 @implementation ViewController
 8 {
 9     NSArray *tableData;
10 }
11 
12 - (void)viewDidLoad
13 {
14     [super viewDidLoad];
15     // Do any additional setup after loading the view, typically from a nib.
16     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];
17     //[tableData retain];
18 
19 }
20 
21 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
22 {
23     return [tableData count];
24     
25 }
26 
27 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
28 {
29     static NSString *simpleTableIdentifier = @"SimpleTableItem";
30     
31     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
32     
33     if (cell == nil) {
34         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
35     }
36     
37     //[[cell textLabel] setText:[tableData objectAtIndex:[indexPath row]]];
38     cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
39     cell.imageView.image = [UIImage imageNamed:@"icon.png"];
40     
41     
42     return cell;
43     
44 }
45 
46 - (void)didReceiveMemoryWarning
47 {
48     [super didReceiveMemoryWarning];
49     // Dispose of any resources that can be recreated.
50 }
51 
52 @end

经过反复的测试后,解决办法如下:

在第17行加上:

[tableData retain];

这样就可以解决报错问题了。