2014-10-28 61 views
1

我在我的數據庫中有7行。我確認所有的數據都通過NSLog成功地傳遞到了iOS端,postArray提供了7.然而,當我運行我的應用程序時,它將只顯示前5行,然後是前2行而不是第6行,從我的數據庫第7行。另外,當我NSLog從我的第6和第7文本視圖的實際文本正確的文本在那裏...爲什麼它重複5行後?謝謝。這裏是我的代碼:爲什麼我的UITableView在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; 
} 

@end

+0

您不要在'cellForRowAtIndexPath'中正確設置您的單元格。您沒有代碼來設置單元格的數據。表格被滾動時,單元格會被重用。因此,每次調用'cellForRowAtIndexPath'都必須使用請求的索引路徑的數據更新單元的狀態。 – rmaddy 2014-10-28 19:46:28

回答

1

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 
    // e.g. cell.textLabel.text = @"Some text"; 

    return cell; 
} 

BTW您可以使用,而不是創建一個新標籤,將其添加爲子視圖作爲的UITableViewCell財產textLabel。如果您需要更多地控制自定義單元格,那麼您應該創建一個自定義類並使用Storyboard文件或Xib文件。

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

在你cellForRowAtIndexPath方法,您要添加子視圖只有當細胞被初始化首次細胞的內容視圖。

相反,您應該將每次調用的子視圖添加/替換爲cellForRowAtIndexPath

相關問題