ios:自定义单元格中的微调器在滚动后消失

问题描述 投票:1回答:4

我创建了一个自定义单元格,其中包含隐藏的微调器和隐藏文本“加载更多”。对于最后一个单元格,这些字段将被取消隐藏,以便用户可以单击它以请求更多数据。我有它,所以当用户点击最后一个单元格时,微调器启动,但是当用户将单元格滚出视图并再次返回时,不会显示微调器。任何帮助将不胜感激。:)

我有一个自定义单元格,它是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

在tableViewController里面我有:

- (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;

}

在didSelectRowAtIndexPath里面我有:

 - (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];
    }
  }
}
ios xcode uitableview uiactivityindicatorview
4个回答
1
投票

添加此代码,即使在停止动画后也显示微调器

 spinner.hidesWhenStopped = NO;

请参阅UIActivityIndicatorView的苹果链接


1
投票

为ce​​llForRowAtIndexPath添加了以下内容:

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

在reloadTableViewData中添加了以下内容:

spinnerIsOn=NO;

在didSelectRowAtIndexPath中添加了以下内容:

spinnerIsOn = TRUE;

0
投票

您还可以子类化layoutSubviews

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

0
投票

我遇到了同样的问题。解:

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
}

当然,spinner.hidesWhenStopped必须是true,默认情况下。

© www.soinside.com 2019 - 2024. All rights reserved.