2012-12-10 52 views
3

我已將UIActivityIndi​​cator的IBOutlet添加到自定義單元格,但它不會顯示在每個單元格上,只是在第2-3個元素上,然後不顯示任何內容。 另一件事,當我滾動表並取回過前3個單元,UIActivityIndi​​cator從這些細胞也消失了......故事板中自定義單元格中的UIActivity指示器

代碼:

Cell.h

@interface Cell : UITableViewCell 

@property (strong, nonatomic) IBOutlet UIImageView *image; 
@property (strong, nonatomic) IBOutlet UILabel *label; 
@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator; 

@end 

Cell.m

@implementation Cell 

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 
    if (self) 
    { 
     // change to our custom selected background view 
    } 
    return self; 
} 

@end 

在故事板UIactivityIndi​​cator設置爲: 行爲: - 動畫

用於測試目的...

和:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CustomCell"; 
    Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    // load the image for this cell 
    Wallpaper *wallpaper = [xmlParser.wallpapers objectAtIndex:indexPath.row]; 

    cell.label.text = wallpaper.title; 
    NSString *imageToLoad = wallpaper.thumbnail; 

    [cell.image setImageWithURL:[NSURL URLWithString:imageToLoad] placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSUInteger receivedSize, long long expectedSize) 
    { 

    } 
          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) 
    { 
     //[cell.activityIndicator stopAnimating]; 
    }]; 

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 
            initWithTarget:self action:@selector(tapped:)]; 
    [cell.image addGestureRecognizer:tap]; 
    [cell.image setTag:indexPath.row]; 

    CALayer * l = [cell.image layer]; 
    [l setMasksToBounds:YES]; 
    [l setCornerRadius:5.0]; 
    cell.layer.cornerRadius = 7.0; 


    return cell; 
} 
+0

您可能還想從代碼而不是從IB創建UIIndicatorView。聽起來像重複使用不正常,或者沒有在IB中分配給細胞。這裏可能會發生一些事情。 –

+0

是的,我知道UITapGestureRecognizer,我試過設置屬性(非原子,強大)UITapGestureRecognizer * tap;到頭文件並將其加載到viewDidLoad中,它不工作;它工作正常,它不適用於每個單元格。 Rob,你有Skype嗎?我真的可以用這個幫助,我堅持了2天。 – 1337code

+0

@MarkM,它被分配到IBOutlet,但只適用於前3個單元格,我會嘗試在代碼中創建它 – 1337code

回答

1
  1. 如果您註釋掉你正在做的setImageWithURL其中的代碼整片,你讓你的動畫活動指示燈成功地在你的表的所有行上?首先確保你的UIActivityIndicatorView工作正常。如果它在你註釋掉setImageWithURL時起作用,那麼問題顯然就在那裏。如果它不起作用,即使您禁用setImageWithURL,那麼您也有一個更基本的問題。讓我們確保您成功啓動活動指標。我收集到你在IB中打開它,但我真的建議在cellForRowAtIndexPath中手動打開它(如果適用),而不是依靠它爲你啓動。

  2. 在已完成的塊,你真的應該檢查,以確保有問題的電池仍然是在屏幕上,並沒有被出隊並重復使用(如調用cell = [tableView cellForRowAtIndexPath:indexPath]並確認非nil值),因爲我假設您的setImageWithURL異步運行。 (注意:這種UITableView實例方法,cellForRowAtIndexPath,不應與名稱相似的UITableViewController方法混淆。)這種有關假定單元格仍在屏幕上的邏輯可能會在您對tableview單元執行異步操作時出現問題。當我聽說有些異步更新沒有正確更新時,我擔心出列的單元格。 (有關如何檢查單元格是否仍然可見的示例,請參閱我在這個Stack Overflow Answer上的第3點的示例;這是一個稍微不同的答案,但給出了您需要考慮的問題的示例。)

  3. 無關,但我不會每次都添加UITapGestureRecognizer。你不是冒着一個被出列並重復使用十幾次輕擊手勢識別器的細胞冒險嗎?最好在你的UITableViewCell子類中完成。我通常在layoutSubviews中執行此操作(如果您嘗試在init方法中執行此操作,將不起作用,因爲imageview將不存在)。

+0

謝謝搶劫!作品真棒! – 1337code

0

嘗試...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    static NSString *CellIdentifier = @"CustomCell"; 
    Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    cell.activityIndicator.hidden = NO; 
    [cell.activityIndicator startAnimating]; 
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:.1]]; 

    // load the image for this cell 
    Wallpaper *wallpaper = [xmlParser.wallpapers objectAtIndex:indexPath.row]; 

    cell.label.text = wallpaper.title; 
    NSString *imageToLoad = wallpaper.thumbnail; 

    [cell.image setImageWithURL:[NSURL URLWithString:imageToLoad] placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSUInteger receivedSize, long long expectedSize) 
    { 

    } 
         completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) 
    { 
     //[cell.activityIndicator stopAnimating]; 
    }]; 

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 
           initWithTarget:self action:@selector(tapped:)]; 
    [cell.image addGestureRecognizer:tap]; 
    [cell.image setTag:indexPath.row]; 

    CALayer * l = [cell.image layer]; 
    [l setMasksToBounds:YES]; 
    [l setCornerRadius:5.0]; 
    cell.layer.cornerRadius = 7.0; 


    return cell; 
} 
0
NINetworkImageView *imageView=[[NINetworkImageView alloc]init]; 
imageView.delegate=self; 
[imageView setPathToNetworkImage:[NSString stringWithStdString:imageUrl]]; 

使用第三方類來解決這個任務。

相關問題