通过字符串填充UITableView?

时间:2014-01-28 22:53:08

标签: ios json string uitableview nsarray

我有一个简单的iOS应用程序,可以解析多个JSON提要并将数据存储在多个字符串中。我确切地知道哪些字符串用于计算的数量和持续时间,因为JSON提要是我从某些网站控制的提要。

但是,即使我在“tableView cellForRowAtIndexPath”方法中指定了这个,UITableView仍然不会填充..

这是因为我使用字符串来填充UITableView吗?如果是这样,你是否必须使用数组来填充UITableView。

谢谢你的时间:)

更新:这是代码:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"Printing table view.");

    static NSString *CellIdentifier = @"Cell";
    AccountCell *cell = (AccountCell *)[account_table dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AccountCell" owner:self options:nil];
        cell = [nib objectAtIndex: 0];

        // Draws the cell background.
        cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"uiFeedletsnglass5.png"]];

        // Draws the pressed cell background.
        cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell-on.png"]];

        // Round of edges of content view in Carousel.
        cell.layer.cornerRadius = 5;
        cell.layer.masksToBounds = YES;
        cell.layer.borderWidth = 1.0f;
        cell.layer.borderColor = [[UIColor grayColor] CGColor];

        cell.profilepic.layer.cornerRadius = 5;
        cell.profilepic.layer.masksToBounds = YES;
        cell.profilepic.layer.borderWidth = 1.0f;
        cell.profilepic.layer.borderColor = [[UIColor grayColor] CGColor];
    }

    if ((facebook_printed == 0) && (logged_facebook == 1)) {
        NSString *full_name = [NSString stringWithFormat:@"%@ %@", facebook_first_name, facebook_last_name];
        cell.username.text = [NSString stringWithFormat:@"%@", full_name];
        cell.account_type_name.text = [NSString stringWithFormat:@"Facebook"];

        NSData *facebook_imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: facebook_proffile_pic]];
        UIImage *facebook_image = [[UIImage alloc] initWithData:facebook_imageData];
        cell.profilepic.image = facebook_image;

        facebook_printed = 1;
    }

    else if ((youtube_printed == 0) && (logged_youtube == 1)) {
        cell.username.text = [NSString stringWithFormat:@"%@", youtube_profilename];
        cell.account_type_name.text = [NSString stringWithFormat:@"YouTube"];

        NSData *youtube_imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: youtube_profilepic]];
        UIImage *youtube_image = [[UIImage alloc] initWithData:youtube_imageData];
        cell.profilepic.image = youtube_image;

        youtube_printed = 1;
    }

    else if ((instagram_printed == 0) && (logged_instagram == 1)) {
        cell.username.text = [NSString stringWithFormat:@"%@", instagram_name_tag];
        cell.account_type_name.text = [NSString stringWithFormat:@"Instagram"];

        NSData *instagram_imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: instagram_profilepicture]];
        UIImage *instagram_image = [[UIImage alloc] initWithData:instagram_imageData];
        cell.profilepic.image = instagram_image;

        instagram_printed = 1;
    }

    else if ((googleplus_printed == 0) && (logged_googleplus == 1)) {
        cell.username.text = [NSString stringWithFormat:@"%@", googleplus_profilename];
        cell.account_type_name.text = [NSString stringWithFormat:@"Google Plus"];

        NSData *googleplus_imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: googleplus_profilepic]];
        UIImage *googleplus_image = [[UIImage alloc] initWithData:googleplus_imageData];
        cell.profilepic.image = googleplus_image;

        googleplus_printed = 1;
    }

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

    return cell;
}

2 个答案:

答案 0 :(得分:0)

一旦获得字符串的值,可能会尝试重新加载表视图的数据吗?

[tableView reloadData];

答案 1 :(得分:0)

经过多次挫折和相当多的反复试验后,我终于发现由于if (cell == nil)我的自定义单元格未加载(显示在UITableView中)。

我根本没有意识到这一点,但从我在网上看到的内容看来,当在Storyboard UI中使用UiTableViews时,你不打算使用控制语句if (cell == nil)

感谢所有评论过这篇文章的人。我感谢你的帮助。