How to set an UIActivityIndicatorView when loading a UITableViewCell

前端 未结 5 948
忘了有多久
忘了有多久 2020-12-28 13:01

I have two UITableViewControllers, A and B. When I tap one cell in table A, I will use UINavigationController to push table view controller B. But

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-28 13:09

    This code below will display a spinner at the footer of the table view if more data is available on the server. You can change it according to your logic of fetching data from server.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         /* set cell attributes here */
         NSInteger lastSectionIndex = [tableView numberOfSections] - 1;
         NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1;
         if ((indexPath.section == lastSectionIndex) && (indexPath.row == lastRowIndex)) {
             if(isMoreDataAvailableOnserver)
             {
                 [self showSpinnerAtFooter];
                 [self getMoreDataFromServer];
             }
             else {
                 [self hideSpinnerAtFooter];
             }
         }
         return cell;
    }
    
    - (void)hideSpinnerAtFooter {
        self.tableView.tableFooterView = nil;
    }
    
    - (void)showSpinnerAtFooter {
        UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [spinner startAnimating];
        spinner.frame = CGRectMake(0, 0, 320, 44);
        self.tableView.tableFooterView = spinner;
    }
    

提交回复
热议问题