0

Everytime I start scrolling in the tableView the subtitleLabel text keeps overlapping each other in every row. I've tried for the last 4 hours every single search on the Internet for this problem I have clicked and tried.

Here is my ViewController.m:

@interface ANViewController()
{

    NSMutableArray *TitleLabel;
    NSMutableArray *SubtitleLabel;
    NSMutableArray *AvatarImages;

}

@end


@implementation ANViewController

@synthesize thetableview;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.thetableview.delegate = self;
    self.thetableview.dataSource = self;

    TitleLabel = [NSMutableArray array];
    SubtitleLabel = [NSMutableArray array];
    AvatarImages = [NSMutableArray array];

    __block NSArray *posts;

    __block NSData *allNewsData = nil;

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0ul);
    dispatch_async(queue, ^{
        allNewsData = [NSData dataWithContentsOfURL:[NSURL URLWithString: API_URL]];
        NSError *error;
        NSMutableDictionary *allNews = [NSJSONSerialization JSONObjectWithData:allNewsData options:NSJSONReadingMutableContainers error:&error];
        if (error) {
            NSLog(@"%@", [error localizedDescription]);
        } else {
            posts = allNews[@"data"];
            for (NSDictionary *newsPost in posts) {
                [TitleLabel addObject:newsPost[@"title"]];
                [SubtitleLabel addObject:newsPost[@"post_message"]];
                NSString *avatarUrl = AVATAR_URL;
                NSString *avatarExt = @".jpg";
                [AvatarImages addObject:[NSString stringWithFormat:@"%@%@%@", avatarUrl, newsPost[@"user_id"], avatarExt]];
            }
        }
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSLog(@"THIS IS THE MAIN THREAD...");
            [self.thetableview reloadData];
        });
    });

    NSURL *url = [NSURL URLWithString: WEBSITE_URL];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [webView loadRequest:urlRequest];
}

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    long count = TitleLabel.count;
    if(count == 0){
        count = 1;
    }
    return count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier;
    ANTableViewCell *Cell;
    if(TitleLabel.count == 0){
        NSLog(@"Did with no news");
        CellIdentifier = @"Cell_NoNews";
        Cell = [thetableview dequeueReusableCellWithIdentifier:CellIdentifier];
        if (Cell == nil) {
            Cell = [[ANTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        Cell.noNewsLabel.text = @"No news to display!";
    }else{
        NSLog(@"Did with news");
        CellIdentifier = @"Cell";
        Cell = [thetableview dequeueReusableCellWithIdentifier:CellIdentifier];
        if (Cell == nil) {
            Cell = [[ANTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        Cell.TitleLabel.text = [TitleLabel objectAtIndex:indexPath.row];
        Cell.SubtitleLabel.text = [SubtitleLabel objectAtIndex:indexPath.row];

        NSURL *url = [NSURL URLWithString:[AvatarImages objectAtIndex:indexPath.row]];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *img = [[UIImage alloc] initWithData:data];
        Cell.AvatarImage.image = img;
    }

    return Cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

1 Answers1

0

Just change UITableViewCellStyleSubtitle for UITableViewCellStyleDefault, and you need an asynchronous connection instead of a synchronous like you're doing: NSData *allNewsData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:API_URL]]; in your viewDidLoad method that is blocking your main thread.

You can adapt this code to your need, in order to download it on the background:

__block NSData *allNewsData = nil;

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
    allNewsData = [NSData dataWithContentsOfURL:[NSURL URLWithString: API_URL]];
NSURL *url = [NSURL URLWithString: WEBSITE_URL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
    dispatch_sync(dispatch_get_main_queue(), ^{
        NSLog(@"THIS IS THE MAIN THREAD...");
        [self.thetableview reloadData];
    });
});
neowinston
  • 7,584
  • 10
  • 52
  • 83