无法在iOS中滚动表格

时间:2017-11-21 06:16:01

标签: ios objective-c iphone uitableview

我在表视图中加载了一个字典数组,在重新加载我的表后,我调用了一个方法,它从数组中更改字典数据,然后刷新/重新加载tableview的特定索引。但除非我的完整数据没有再次更新,否则我无法滚动表格。

我调用方法和重新加载表的代码:

- (void)parserDidEndDocument:(NSXMLParser *)parser {
    [Hud hideAnimated:true];
    [self.myTbl reloadData];
    dispatch_async(dispatch_get_main_queue(),^{
         [self loadTutorials];
    });
}

我正在尝试更新表数据的方法。

#pragma mark:GetAllData
-(void)loadTutorials{
  dispatch_async(dispatch_get_main_queue(),^{


    int k = 0;

  for (NSMutableString*linknew in linkArr) {
     //here some calculations code for parsing then next  
      for (TFHppleElement *elements in contributorsNodes) {
        // 5
        for (TFHppleElement *child in elements.children) {
            if ([child.tagName isEqualToString:@"img"]) {
                // 7
                @try {
                    NSString*url = [child.attributes objectForKey:@"src"];

                    NSMutableDictionary*dict = [[feeds objectAtIndex:k] mutableCopy];
                    [dict setObject:url forKey:@"image"];
                    [feeds removeObjectAtIndex:k];
                    [feeds insertObject:dict atIndex:k];

                    NSIndexPath*index = [NSIndexPath indexPathForRow:k inSection:0];

                    [self.myTbl reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationNone];


                }
                @catch (NSException *e) {}
            }
        }
    }k++;
  }
  });
}

cellForRowAtIndexPath方法:

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

    UILabel*lbl = (UILabel*)[cell viewWithTag:101];
    UIImageView*imgView = (UIImageView*)[cell viewWithTag:103];


    [imgView sd_setImageWithURL:[NSURL URLWithString:[[feeds objectAtIndex:indexPath.row] objectForKey:@"image"]] placeholderImage:[UIImage imageNamed:@"img.jpg"]];

    lbl.text = [NSString stringWithFormat:@"%@",[[feeds objectAtIndex:indexPath.row] objectForKey: @"title"]];

    return cell;
}

1 个答案:

答案 0 :(得分:0)

你的问题中有很多东西让我感到困惑。

  • 使用parserDidEndDocument方法,我不知道为什么你需要致电[self.myTbl reloadData],但之后你会在self.myTbl方法中更新loadTutorials
  • 为什么你需要在dispatch_async方法的外部和内部调用2 loadTutorials?我认为没有必要。只需拨打一次电话。
  • 要替换数组中的对象,您不需要删除和插入对象。只需使用feeds[k] = dict替换即可。使用前请确保feeds.count > k
  • 正如我猜的那样,您使用try catch因为在self.myTbl方法中更新loadTutorials时遇到了崩溃。在我看来,您应该使用beginUpdatesendUpdates代替try catch

我认为问题是因为您使用try catch来处理崩溃。

尝试用@Losiowaty建议的后台队列重写代码,我们将

- (void)parserDidEndDocument:(NSXMLParser *)parser {
  [Hud hideAnimated:true];
  [self.myTbl reloadData];
  [self loadTutorials];
}



#pragma mark:GetAllData
-(void)loadTutorials{
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    int k = 0;

    for (NSMutableString*linknew in linkArr) {
      //here some calculations code for parsing then next
      for (TFHppleElement *elements in contributorsNodes) {
        // 5
        for (TFHppleElement *child in elements.children) {
          if ([child.tagName isEqualToString:@"img"]) {
            NSString*url = [child.attributes objectForKey:@"src"];

            // I assume in some case, feeds[k] doesn't exist. We need add an
            // NSMutableDictionary to this index before using to avoid crash.
            // If you make sure feeds[k] always exist you can ignore it

            if (feeds.count == k) {
              [feeds addObject:[[NSMutableDictionary alloc] init]];
            }
            [feeds[k] setObject:url forKey:@"image"];

            NSIndexPath*index = [NSIndexPath indexPathForRow:k inSection:0];

            dispatch_async(dispatch_get_main_queue(), ^{
              [self.myTbl beginUpdates];
              [self.myTbl reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationNone];
              [self.myTbl endUpdates];
            });
          }
        }
      }k++;
    }
  });
}

首先尝试代码。如果您还有任何问题,请告诉我。我会帮助你的。