2012-07-31 2 views
0

테이블 뷰에 일부 데이터를로드해야하는데이 백그라운드에서 진행되는 동안 진행 상태가 있음을 보여주기 위해 작업 표시기를 추가하려고합니다. 끝내라. 이와 같은 것을 구현하는 가장 효율적인 방법은 무엇입니까?UITableView에 UIActivityIndicator 추가하기

답변

1

사용자를 차단할지 여부와 활동 표시의 중요도에 따라 다릅니다.

당신은 당신이 더 큰 활동 표시를 갖고 싶어하고 여전히 사용자를 차단 테이블 뷰 (tableview.height -= activityview.height) 아래의 텍스트와 UIActivityIndicator와 UIView의 애니메이션을하지 않을 경우, Application.networkActivityIndicatorVisible를 사용하여 사용자를 차단하려면 다음 전체에 숨길 수없는 경우 또는 사용자를 차단하려면 차단 활동 표시기를 사용하십시오.

  • http://www.dejal.com/developer/?q=developer/dsactivityview
  • https://github.com/jdg/MBProgressHUD

      는 (개인적으로 MBProgressHUD를 사용했고 그것은 배우고 사용하기 쉽습니다)
  • +0

    나는 확실히 그 링크를 좋아합니다. 제게 이것들을 보여 줘서 고마워요. –

    +0

    그런데 어떻게 MBProgressHud 프로젝트를 앱에 추가 했습니까? 컴파일 할 때마다 컴파일 오류가 발생합니다. –

    +0

    어떤 종류의 컴파일 오류입니까? ARC를 사용하고 있다면, MBProgressHUD ('Target'>'Build Phases'>'Compilation Sources'>'MBProgressHUD.m' 컴파일러 플래그'-fno-objc-arc')에 대해 ARC를 비활성화해야합니다. –

    0

    당신은 셀의 하위 뷰 같은 UIIndicatorView와 UILabel의가있는 뷰를 추가 할 수 있습니다. 당신은 ... 오류 데이터 로딩/오류 네트워크/빈 데이터를 표시하려면이 방법을 사용할 수 있습니다

    예 : UITableViewModeMessage 및 UITableViewModeData : 두 가지 모드를 정의 할 수 있습니다

    컨트롤러.

    viewDidLoad에서 self.tableViewMode = UITableViewModeMessage를 설정합니다. 데이터가 반환되면 self.tableViewMode = UITableViewModeData를 설정하고 tableview에 대한 데이터를 다시로드하십시오.

    일부 코드 :

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
        if (self.tableViewMode == UITableViewModeMessage) { 
         return 2; 
        } else { 
         return self.yourEntries ? self.yourEntries.count : 0; 
        } 
    } 
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {  
        if (self.tableViewMode == UITableViewModeMessage) { 
         return [self tableView:tableView messageCellForRowAtIndexPath:indexPath]; 
        } else { 
         return [self tableView:tableView dataCellForRowAtIndexPath:indexPath]; 
        } 
    } 
    
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    // Remove Loading... progress view if exist. 
        UIView *progressView = [cell viewWithTag:100]; 
        [progressView removeFromSuperview]; 
    
        if (self.tableViewMode == UITableViewModeMessage) {   
         if (indexPath.row == 1) { 
          // remove the current label. 
          cell.textLabel.text = nil; 
    
          // We build progress view and attach to cell here but not in cellForRowAtIndexPath is because in this method cell frame is already calculated. 
          UIView *progressView = [self progressViewForCell:cell message:@"Loading..." alpha:0.9]; 
          [cell addSubview:progressView]; 
         } 
        } 
    } 
    
    
    // cell to display when loading 
    - (UITableViewCell *)tableView:(UITableView *)tableView messageCellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        static NSString *CellIdentifier = @"MessageCell"; 
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
        if (cell == nil) { 
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
         cell.textLabel.textColor = [UIColor grayColor]; 
         cell.textLabel.textAlignment = UITextAlignmentCenter; 
        } 
    
        if (indexPath.row == 1) { 
         cell.textLabel.text = @"Loading..."; 
        } else { 
         cell.textLabel.text = nil; 
        } 
    
        return cell; 
    } 
    
    // cell to display when has data 
    - (UITableViewCell *)tableView:(UITableView *)tableView dataCellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {  
        static NSString *CellIdentifier = @"DataCell"; 
    
        if (cell == nil) { 
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
        } 
    
        cell.textLabel.text = [[self.yourEntries objectAtIndex:indexPath.row] description]; 
    
        return cell; 
    } 
    
    // Build a view which has a UIActivityIndicatorView and a UILabel 
    - (UIView *)progressViewForCell:(UITableViewCell *)cell message:(NSString *)message alpha:(CGFloat)alpha 
    { 
        // NOTE: progressView needs to be removed from cell in cellForRowAtIndexPath: 
        CGRect progressViewFrame = CGRectZero; 
        progressViewFrame.size.width = CGRectGetMaxX(cell.bounds); 
        progressViewFrame.size.height = CGRectGetMaxY(cell.bounds) - 2; 
    
        UIView *progressView = [[UIView alloc] initWithFrame:progressViewFrame]; 
        progressView.backgroundColor = RGBA(255, 255, 255, 1); 
        progressView.alpha = alpha; 
        progressView.tag = 100; 
    
        UILabel *loadingLabel = [[UILabel alloc] initWithFrame:progressView.bounds]; 
        loadingLabel.backgroundColor = [UIColor clearColor]; 
        loadingLabel.font = [UIFont systemFontOfSize:14]; 
        loadingLabel.textColor = [UIColor blackColor]; 
        loadingLabel.textAlignment = UITextAlignmentCenter; 
        loadingLabel.text = message; 
    
        CGFloat widthOfText = [loadingLabel.text sizeWithFont:loadingLabel.font].width; 
        CGFloat spaceBetweenIndicatorAndLabel = 5; 
    
        // activityIndicatorView has size in which width and height is equal to 20. 
        UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
        [activityIndicatorView setCenter:CGPointMake(CGRectGetMidX(cell.bounds) - (widthOfText/2) - (activityIndicatorView.bounds.size.width/2) - spaceBetweenIndicatorAndLabel, CGRectGetMidY(cell.bounds))]; 
        [activityIndicatorView setColor:[UIColor blackColor]]; 
        [activityIndicatorView startAnimating]; 
    
        [progressView addSubview:activityIndicatorView]; 
        [progressView addSubview:loadingLabel]; 
    
        return progressView; 
    } 
    
    관련 문제