我的UITableView为何在5行之后重复行? [英] How come my UITableView is repeating rows after 5 rows?

查看:75
本文介绍了我的UITableView为何在5行之后重复行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有7行.我通过NSLog postArray确认所有数据已成功传递到iOS端,该数组提供了7.但是,当我运行我的应用程序时,它将仅显示前5行,然后显示前2行,而不是第6行和第6行.我数据库的第7行.另外,当我NSLog从第6个和第7个文本视图查看实际文本时,正确的文本就在其中...为什么在5行之后重复出现?谢谢你.这是我的代码:

I have 7 rows in my database. I confirmed that all of the data is successfully coming over to the iOS side by NSLog the postArray which gives 7. However when I run my app, it will only display the first 5 rows, and then the first 2 rows instead of the 6th and 7th row from my database. Also when I NSLog the actual text from my 6th and 7th text view the correct text is in there... Why is it repeating after 5 rows? Thank you. Here is my code:

#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

推荐答案

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

UITableView reuses the cells with the same reuse identifier, which means that when you scroll so that a row is scrolled out of the visible area, it will be used to show new rows. That's why when your 1st and 2nd rows were scrolled out of the view, they were used to show the 5th and 6th row.

您需要修改加载单元的方式.

You need to modify how you load cells.

- (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;
}

顺便说一句,您可以使用UITableViewCell的属性textLabel代替创建新标签并将其添加为子视图.如果您需要更好地控制自定义单元,则应创建一个自定义类,并使用一个Storyboard文件或Xib文件.

BTW You can use the property textLabelof UITableViewCellinstead of creating a new label and adding it as as subview. If you need to have more control over your custom cell, then you should create a custom class and use a Storyboard file or a Xib file.

这篇关于我的UITableView为何在5行之后重复行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆