我有关于UIActivity Indicator的问题,我在UITableView Cell上实现了Activity指示器,但它无法正常工作。我的代码如下,如果可以解决,请告诉我我在做的错误。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIActivityIndicatorView *indicator=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
if (segmentControl.selectedSegmentIndex==0)
{
if ([indexPath row]==[FriendSuggestion count])
{
int cursor=[Constants getCursorForKey:@"friendList_cursor"];
if (cursor!=0)
{
[[tableView cellForRowAtIndexPath:indexPath] addSubview:indicator];
[indicator setFrame:CGRectMake(40, 25, 37, 37)];
[NSThread detachNewThreadSelector:@selector(startAnimating) toTarget:indicator withObject:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[self loadingMore:indexPath :indicator :cursor];
});
}else
isNoMoreData=YES;
[tableView reloadData];
}
}
else
{
if ([indexPath row]==[popularSuggestion count])
{
int cursor=[Constants getCursorForKey:@"popularList_cursor"];
if (cursor!=0)
{
[[tableView cellForRowAtIndexPath:indexPath] addSubview:indicator];
[indicator setFrame:CGRectMake(40, 25, 37, 37)];
[NSThread detachNewThreadSelector:@selector(startAnimating) toTarget:indicator withObject:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[self loadingMore:indexPath :indicator :cursor];
});
}else
isNoMoreData=YES;
[tableView reloadData];
}
}
}
-(void)loadingMore:(NSIndexPath*)indexPath:(UIActivityIndicatorView*)indicator:(int)cursor
{
NSLog(@"loading more called ...");
if (segmentControl.selectedSegmentIndex==0)
{
isNoMoreData=NO;
NSArray *nextArray=[[NSArray alloc] init];
nextArray=[Constants getFriendsSuggestionsStr:cursor];
for (NSDictionary *dic in nextArray) {
[FriendSuggestion addObject:dic];
}
}else
{
isNoMoreData=NO;
NSArray *nextArray=[[NSArray alloc] init];
nextArray=[Constants getPopularSuggestionsStr:cursor];
for (NSDictionary *dic in nextArray) {
[popularSuggestion addObject:dic];
}
}
[table reloadData];
[indicator stopAnimating];
}
先谢谢..!
答案 0 :(得分:1)
应该在主线程上执行UIKit方法,不应该在主线程上调度你的加载方法。
而且,或许可以帮助更好一点:什么不起作用?错误?指标没有显示?崩溃?
在没有NSThread电话的情况下开始为您的指标设置动画。
[indicator startAnimating];
然后为您的数据加载创建一个队列
dispatch_queue_t loadingQueue = dispatch_queue_create("loading Queue", NULL);
dispatch_async(loadingQueue, ^{
// loading stuff
}
dispatch_release(loadingQueue);
答案 1 :(得分:1)
如果你说作为子视图添加的标签正在处理其中没有应用任何条件的其他行 。然后它可能会被添加到您的单元格中,重新加载表后它会消失。尝试使用您的方法进行一些更改:
if (segmentControl.selectedSegmentIndex==0)
{
if ([indexPath row]==[FriendSuggestion count])
{
int cursor=[Constants getCursorForKey:@"friendList_cursor"];
if (cursor!=0)
{
[[tableView cellForRowAtIndexPath:indexPath] addSubview:indicator];
[indicator setFrame:CGRectMake(40, 25, 37, 37)];
[NSThread detachNewThreadSelector:@selector(startAnimating) toTarget:indicator withObject:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[self loadingMore:indexPath :indicator :cursor];
});
}else
{
[tableView reloadData];
isNoMoreData=YES;
}
}
}
在else块中重新加载表...
答案 2 :(得分:0)
为什么要在另一个线程上启动指标?使用UI进行Evreyhting应该在主线程上完成。
顺便尝试将指示符添加到单元格中。您必须将其添加到
cell.contentView
这是错误的:
[[tableView cellForRowAtIndexPath:indexPath] addSubview:indicator]
应该
[[[tableView cellForRowAtIndexPath:indexPath] contentView] addSubview:indicator]