wwjdm
wwjdm

Reputation: 2596

ios:Spinner in custom cell disappears after scrolling

I created a custom cell with a hidden spinner and hidden text "Load More". These fields are unhidden for the last cell so that the user can click it to request more data. I have it woking so that when the user clicks the last cell the spinner starts, but when the user scrolls the cell out of view and back again the spinner is not shown. Any help will be appreciated.:)

I have a custom cell that is a subclass of UITableViewCell:

@interface BrowseListCell : UITableViewCell{
IBOutlet UIImageView *image;
IBOutlet UILabel *name;
IBOutlet UILabel *loadMore;
IBOutlet UIActivityIndicatorView *spinner;
}
@property(nonatomic, retain) UIImageView *image;
@property(nonatomic, retain) UILabel *name;
@property(nonatomic, retain) UILabel *loadMore;
@property(nonatomic, retain) UIActivityIndicatorView *spinner;
@end

Inside the tableViewController I have:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
return [_dataController countOfList]+1;
}

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

// Configure the cell...
static NSString *CellIdentifier = @"BrowseListCell";

BrowseListCell *cell = (BrowseListCell *)[tableView     dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
    cell = [_cell autorelease];
    _cell = nil;
}
if(indexPath.row==[_dataController countOfList]){
    if (indexPath.row==0) {
        cell.name.text=nil;
        cell.image.image = nil;
        cell.loadMore.hidden = YES;
        return cell;
    }else{
        cell.name.text=nil;
        cell.image.image = nil;
        cell.loadMore.hidden = NO;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        _spinner = [cell.spinner retain];
        _loadCell = [cell retain];
        return cell;
    }
}
if(_dataController!=nil){
    BrowseProduct *productAtIndex = [_dataController objectInListAtIndex:indexPath.row];

    // Configure the cell...
    if(productAtIndex!=nil){
        cell.name.text = productAtIndex.name;
        cell.image.image = productAtIndex.image;
        cell.loadMore.hidden=YES;
    }

}

return cell;

}

Inside didSelectRowAtIndexPath I have :

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==[_dataController countOfList]){
    if (indexPath.row==0) {
        //do nothing
    }else{
        NSLog(@"Loading");
        [_spinner startAnimating];
        _loadCell.userInteractionEnabled = NO;
        NSString *startFromId = [NSString stringWithFormat:@"%d", [_dataController countOfList]];
        NSNotification *notification = [NSNotification notificationWithName:@"loadMore" object:startFromId];
        [[NSNotificationCenter defaultCenter]postNotification:notification];
    }
  }
}

Upvotes: 1

Views: 1475

Answers (4)

Kasra Babaei
Kasra Babaei

Reputation: 307

I encountered the same problem. Solution:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    cell.spinner.startAnimating()

    // populate data, for instance: download an image from the net

    cell.spinner.stopAnimating()

    return cell
}

And of course, spinner.hidesWhenStopped must be true, which is by default.

Upvotes: 0

Serluca
Serluca

Reputation: 2102

You can also subclass layoutSubviews

- (void)layoutSubviews{
    [super layoutSubviews];
    [self.spinner startAnimating];
}

Upvotes: 0

wwjdm
wwjdm

Reputation: 2596

Added the following for cellForRowAtIndexPath:

if(spinnerIsOn){
            [_spinner startAnimating];
        }else{
            [_spinner stopAnimating];
        }

Added following to reloadTableViewData:

spinnerIsOn=NO;

Added the following to didSelectRowAtIndexPath:

spinnerIsOn = TRUE;

Upvotes: 1

Vinodh
Vinodh

Reputation: 5268

Add this code , to show spinner even after stop animating

 spinner.hidesWhenStopped = NO;

Please refer the apple link for UIActivityIndicatorView

Upvotes: 1

Related Questions