-1

我有一个关于 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];
}

提前致谢..!

4

3 回答 3

1

UIKit 方法应该在主线程上执行,你的加载方法不应该在主线程上调度。

而且,也许可以帮助更好一点:什么不起作用?错误?指标不显示?崩溃?

在没有 NSThread 调用的情况下开始为您的指标设置动画。

[indicator startAnimating];

然后为加载数据创建一个队列

dispatch_queue_t loadingQueue = dispatch_queue_create("loading Queue", NULL);
dispatch_async(loadingQueue, ^{
 // loading stuff
}
dispatch_release(loadingQueue);
于 2012-06-02T09:50:35.550 回答
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 块中重新加载表...

于 2012-06-02T12:11:58.587 回答
0

为什么要在另一个线程上启动指标?使用 UI 的 Evreyhting 应该在主线程上完成。

顺便说一句,尝试将您的指标添加到单元格中。您必须将其添加到

cell.contentView

这是错误的:

[[tableView cellForRowAtIndexPath:indexPath] addSubview:indicator]

应该

[[[tableView cellForRowAtIndexPath:indexPath] contentView] addSubview:indicator]

于 2012-06-02T09:46:43.600 回答