为什么我的UITableView在5行之后重复行?

我有7行在我的数据库。 我确认,所有的数据都成功地通过NSLog的postArray给出7的iOS端。然而,当我运行我的应用程序,它将只显示前5行,然后前2行,而不是第6和第7行从我的数据库。 另外,当我从我的第六和第七文本查看实际的文本查看正确的文本在那里…为什么它重复5行后? 谢谢。 这是我的代码:

#import "DailyViewController.h" #import "Post.h" @interface DailyViewController () @end @implementation DailyViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // View background [self.view setBackgroundColor:[UIColor colorWithRed:(255/255.0) green:(221/255.0) blue:(85/255.0) alpha:1.0f]]; NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://example.org/getPosts.php"]]; NSData *data = [NSData dataWithContentsOfURL:url]; jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; postArray = [[NSMutableArray alloc] init]; for(int i = 0; i < jsonArray.count; i++) { NSString *nickname = [[jsonArray objectAtIndex:i] objectForKey:@"nickname"]; NSString *squeal = [[jsonArray objectAtIndex:i] objectForKey:@"squeal"]; [postArray addObject:[[Post alloc] initWithNickname:nickname andSqueal:squeal]]; } viewArray = [[NSMutableArray alloc] init]; for(int i = 0; i < postArray.count; i++) { Post *postObject; postObject = [postArray objectAtIndex:i]; UILabel *nicknameLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 15, 320, 30)]; nicknameLabel.text = postObject.nickname; nicknameLabel.font = [UIFont boldSystemFontOfSize:20]; UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(25, 42, 320, 0)]; textView.font = [UIFont systemFontOfSize:15]; textView.text = postObject.squeal; textView.editable = false; [textView setScrollEnabled:false]; [textView sizeToFit]; [textView layoutIfNeeded]; UIView *view = [[UIView alloc] initWithFrame: CGRectMake (0, 0, 320, 30+textView.frame.size.height)]; [view addSubview:nicknameLabel]; [view addSubview:textView]; [viewArray addObject:view]; } [self.tableView reloadData]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return postArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; [cell.contentView addSubview:[viewArray objectAtIndex:indexPath.row]]; } return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { UIView *view = [viewArray objectAtIndex:indexPath.row]; return view.frame.size.height+30; } 

@结束

UITableView重复使用具有相同重用标识符的单元格,这意味着当您滚动以使行滚出可见区域时,将用于显示新行。 这就是为什么当你的第一排和第二排从视图中滚动出来时,它们被用来显示第五和第六排。

您需要修改如何加载单元格。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; // Add and setup views here. } //Add content to view here // eg cell.textLabel.text = @"Some text"; return cell; } 

顺便说一句你可以使用UITableViewCell属性textLabel而不是创build一个新的标签,并将其添加为子视图。 如果您需要更多地控制自定义单元格,则应该创build一个自定义类并使用Storyboard文件或Xib文件。

 if(cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } else { [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; } [cell.contentView addSubview:[viewArray objectAtIndex:indexPath.row]]; 

在您的cellForRowAtIndexPath方法中,只有在单元格初次初始化时,才会将子视图添加到单元格的内容视图中。

相反,您应该将每个调用的子视图添加/replace为cellForRowAtIndexPath