2014-05-14 2 views
0

서버에서 콘텐츠를로드하는 UITableView가 있습니다. 모두 잘 작동하고 내용이 올바르게로드되지만 UITableView를 스크롤하면 약간 지연됩니다. 서버에서 콘텐츠를 다운로드하는 메서드를 만들고이 메서드를 내 viewDidLoad 메서드에서 호출합니다.이 메서드는 앱을 열 때 가장 먼저 열리기 때문에이 메서드를 호출합니다. 이 방법이 최선의 방법인지 잘 모르겠습니다. 어떻게 피할 수 있습니까? 여기 내 코드는 다음과 같습니다.UITableView Scroll Lag

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Method that Loads the Content 
    [self retrieveData]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Retorna o número de linhas pelo array de programacao. 
    return programacaoArray.count; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

    // ALTURA da TableViewCell 
    return 150; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *identifier = @"CellIdentifier"; 
    ProgramacaoTableCell *cell = (ProgramacaoTableCell *)[self.tableView dequeueReusableCellWithIdentifier:identifier]; 

    if (cell == nil) { 
     cell = [[ProgramacaoTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 
    } 

    // Configure the Cell 
    Programacao *programacaoObject; 
    programacaoObject = [programacaoArray objectAtIndex:indexPath.row]; 

    cell.atracaoImagem.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:programacaoObject.programacaoImagem]]]; 
    cell.nomeLabel.text = programacaoObject.programacaoNome; 
    cell.dataLabel.text = programacaoObject.programacaoData; 
    cell.localLabel.text = programacaoObject.programacaoLocal; 

    return cell; 

} 

- (void) retrieveData 
{ 

    NSURL *url = [NSURL URLWithString:getDataURL]; 
    NSData *data = [NSData dataWithContentsOfURL:url]; 

    jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 

    // SETUP programacaoArray 
    programacaoArray = [[NSMutableArray alloc] init]; 

    // Loop throught do Array 
    for (int i = 0; i < jsonArray.count; i++) { 
     // Cria o PROGRAMACAO Objeto 
     NSString *pID   = [[jsonArray objectAtIndex:i] objectForKey:@"id"]; 
     NSString *pNome  = [[jsonArray objectAtIndex:i] objectForKey:@"programacaoNome"]; 
     NSString *pImagem  = [[jsonArray objectAtIndex:i] objectForKey:@"programacaoImagem"]; 
     NSString *pDescricao = [[jsonArray objectAtIndex:i] objectForKey:@"programacaoDescricao"]; 
     NSString *pData  = [[jsonArray objectAtIndex:i] objectForKey:@"programacaoData"]; 
     NSString *pLocal  = [[jsonArray objectAtIndex:i] objectForKey:@"programacaoLocal"]; 
     NSString *pPrecos  = [[jsonArray objectAtIndex:i] objectForKey:@"programacaoPrecos"]; 

     // Add to the Array 
     [programacaoArray addObject:[[Programacao alloc] initWithNome:pNome andImagem:pImagem andDescricao:pDescricao andData:pData andLocal:pLocal andPrecos:pPrecos andID:pID]]; 
    } 

    // RELOAD TABLE VIEW 
    [self.tableView reloadData]; 
} 
+0

이'programacaoObject.programacaoImagem' 문자열의 유형을 포함 않습니다 로딩 이미지를 사용할 수 있습니까? 그것은 'http : //'링크입니까? – Michael

+0

그는 질문에서 그가 서버에서 콘텐츠를 다운로드한다고 말하면 내 추측은 '예'라고합니다. –

+0

그리고이 '예'또한 질문의 답이 될 것입니다;) – Michael

답변

2

아주 명백한 실수가 있습니다. 이미지를 동 기적으로로드하는 경우 (동일한 스레드 (이 경우 메인 스레드)), 지연을 유발하는 원인이됩니다. 당신은 AFNetworkingUIImageView 카테고리를 조사해야합니다. 그러면 이미지가 백그라운드에서로드됩니다.

AFNetworking 라이브러리는 here에서 다운로드 할 수 있습니다. 'cell.atracaoImagem.image = [있는 UIImage imageWithData : [을 NSData dataWithContentsOfURL : [NSURL URLWithString :

+0

참고로, AFNetworking을 사용하여 completionData 메소드에서'[self.tableView reloadData];를 호출하여'retrieveData'에서 다운로드 작업을 실행할 수 있습니다. 요점은 메인 스레드에서 절대 다운로드하지 않는 것입니다. –

-1

내가 대신 내가 코드에 의한 원인 문제를 생각 viewDidLoad

+0

그의 문제는 전체보기를로드하는 것이 아니라 테이블보기를 스크롤하는 데 있습니다. 그가해야 할 일은 백그라운드에서'retrieveData'를 실행하는 것입니다 ... –

0

retrieveData 기능 viewWillAppear의 메소드를 호출해야한다고 생각 programacaoObject을 .programacaoImagem]]];

당신은 SDWebImage 프레임 워크,