[英]How to stop activity indicator?
如何停止UIActivityIndicatorView? 此代碼無效。
BubbleTableViewCell.h:
@interface BubbleTableViewCell : UITableViewCell {
UIActivityIndicatorView *activity;
}
BubbleTableViewCell.m:
- (void)setActivity:(BOOL)value {
if (value) {
activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activity.center = CGPointMake(15, 15);
[activity startAnimating];
[self.contentView addSubview:activity];
}
else {
[activity stopAnimating];
[activity removeFromSuperview];
[activity release];
}
}
我只能假設您在何處以及為什么需要活動指示器,但是如果在數據加載期間需要活動指示器,則可以這樣做:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell;
MyCustomCell * myCell = [TableViewCellsFactory loadCellFromNibWithName:@"myCell" identifier:@"myCell" tableView:aTableView owner:self];
if(dataIsLoading) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"loadingCell"] autorelease];
UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[activityIndicator startAnimating];
[activityIndicator setFrame:CGRectMake(140, 40, 40, 40)];
[cell addSubview:activityIndicator];
}
else {
//here you customize myCell
cell = myCell;
}
return cell;
這對我來說很完美。
兩種情況下都只調試值的狀態。 我認為將BOOL值設置為true不會更改為false來執行其他部分。
您是否設置了兩次活動? 一種可能是,如果使用相同的BOOL值兩次調用此函數,則會丟失參考
假設您要在下載某些數據(例如圖像)時在UITableViewCell中顯示活動指示器,並且一旦下載了該指示器,您就想停止該指示器。 在tableView:cellForRowAtIndexPath:
方法中執行此操作的一種方法是:
setActivity:
方法,參數中為NO
。 setActivity:
方法,並在參數中輸入YES
,然后開始下載相應的數據。 - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
tableView:cellForRowAtIndexPath:
,將下載該行的數據,並使用參數中的NO
調用setActivity:
方法,這將隱藏指示符(如果有)。 使用這種方法,您必須對函數進行一些修改以:
- (void)setActivity:(BOOL)value
{
if (value)
{
activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activity.center = CGPointMake(15, 15);
[activity startAnimating];
[self.contentView addSubview:activity];
}
else
{
if(activity)
{
[activity stopAnimating];
[activity removeFromSuperview];
[activity release];
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.