55

解析されたデータをセルに読み込もうとしていますが、問題はそれが同期的に行われており、データの読み込みが完了するまで UitableView が表示されないことです。performSelectorInBackground を使用して問題を解決しようとしましたが、スクロールを開始するまでデータがセルに読み込まれません。これが私のコードです:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self performSelectorInBackground:@selector(fethchData) withObject:nil];


}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
self.listData = nil;
    self.plot=nil;
}


-(void) fethchData

{
    NSError *error = nil;
    NSURL *url=[[NSURL alloc] initWithString:@"http://www.website.com/"];
    NSString *strin=[[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

    HTMLParser *parser = [[HTMLParser alloc] initWithString:strin error:&error];

    if (error) {
        NSLog(@"Error: %@", error);
        return;
    }

    listData =[[NSMutableArray alloc] init];
    plot=[[NSMutableArray alloc] init];

    HTMLNode *bodyNode = [parser body];
    NSArray *contentNodes = [bodyNode findChildTags:@"p"];


    for (HTMLNode *inputNode in contentNodes) {
            [plot addObject:[[inputNode allContents] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];    
        }


    NSArray *divNodes = [bodyNode findChildTags:@"h2"];

    for (HTMLNode *inputNode in divNodes) {

            [listData addObject:[[inputNode allContents] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];          

        }
    }


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

    static NSString *CellIdentifier = @"Cell";
    //here you check for PreCreated cell.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }

    //Fill the cells...  


    cell.textLabel.text = [listData objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
    cell.textLabel.numberOfLines=6; 
    cell.textLabel.textColor=[UIColor colorWithHue:0.7 saturation:1 brightness:0.4 alpha:1];


    cell.detailTextLabel.text=[plot objectAtIndex:indexPath.row];
    cell.detailTextLabel.font=[UIFont systemFontOfSize:11];
    cell.detailTextLabel.numberOfLines=6;

    return cell;


}
4

8 に答える 8

16

迅速な 3 の場合:

DispatchQueue.main.async(execute: { () -> Void in
                self.tableView.reloadData()
            })

迅速な 2 の場合:

dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.tableView.reloadData()
            })
于 2016-03-29T22:01:28.063 に答える
9

あなたが本当にする必要があるのは、バックエンドデータを更新するときはいつでも、電話することです

[tableView reloadData];

これは同期的に発生するため、おそらく次のような関数が必要です。

-(void) updateTable
{
    [tableView reloadData];
}

ダウンロードコールにデータを追加した後

[self performSelectorOnMainThread:@selector(updateTable) withObject:nil waitUntilDone:NO];
于 2012-06-04T17:17:45.180 に答える
4

[cell setNeedsDisplay] を使用できます。例えば:

dispatch_async(dispatch_get_main_queue(), ^{
            [cell setNeedsDisplay];
            [cell.contentView addSubview:yourView];
        });
于 2015-05-19T08:55:02.473 に答える
1

私はまったく同じ問題を抱えていました!ビュー コントローラーが表示される前に、UITableView を完全に設定したかったのです。Envil の投稿は私が必要としていた情報を提供してくれましたが、私の解決策は違ったものになってしまいました。

これが私がやったことです(質問者の文脈に合うように改造されています)。

- (void)viewDidLoad {
    [super viewDidLoad];
    [self performSelectorInBackground:@selector(fethchData) withObject:nil];
}

- (void)viewWillAppear {
    [tableView reloadData];
}
于 2014-11-19T19:59:43.477 に答える
0

まず、これは私の関連する問題を半解決しました。表セルの画像の角を丸くしたい。非同期にディスパッチすると、一部の画像の問題が修正されましたが、すべての画像では修正されませんでした。何か案は?

第二に、次のようなクロージャ キャプチャ リストを使用して、強い参照サイクルを作成しないようにする必要があると思います。

DispatchQueue.main.async(execute: { [weak weakSelf = self] () -> Void in
            weakSelf!.tableView.reloadData()
        })

参照: https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html#//apple_ref/doc/uid/TP40014097-CH20-ID52

于 2016-10-14T00:03:48.793 に答える