在后台获取数据,但表视图不更新

时间:2013-07-22 10:58:09

标签: iphone ios6

我想立即使用推送视图控制器切换到下一个视图,因此使用以下方法从Web服务获取数据。但这不是更新UITableView。

//   vewDidLoad

[NSThread detachNewThreadSelector:@selector(setImage) toTarget:self withObject:nil];

-(void)setImage
{    
            if([type isEqualToString:@"Organisation"])
            {
                self.mGetDataDict = [MyEventApi members:self.mUserIdDict];
                self.mRecievedDataDict = [self.mGetDataDict valueForKey:@"members"];
            }
            if([type isEqualToString:@"Individual"]){
                self.mGetDataDict = [MyEventApi friends:self.mUserIdDict];
                self.mRecievedDataDict = [self.mGetDataDict valueForKey:@"friends"];
            }

            if([self.mGetDataDict valueForKey:@"friends"] == [NSNull null])
            {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"alert" message:@"You have not added any friend yet." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
            }
            else
            {
                self.mFrndNameArr = [[NSMutableArray alloc]init];
                self.mFrndImgArr = [[NSMutableArray alloc]init];
                self.mFirstNameArr = [[NSMutableArray alloc]init];
                self.mLastNameArr = [[NSMutableArray alloc]init];
                self.mFrndIdArr = [[NSMutableArray alloc]init];
                self.mFrndMSinceArr = [[NSMutableArray alloc]init];
                self.mFrndDescArr = [[NSMutableArray alloc]init];

                self.mFrndNameArr = [self.mRecievedDataDict valueForKey:@"username"];
                self.mFrndImgArr = [self.mRecievedDataDict valueForKey:@"image"];
                self.mFirstNameArr = [self.mRecievedDataDict valueForKey:@"firstName"];
                self.mLastNameArr = [self.mRecievedDataDict valueForKey:@"lastName"];
                self.mFrndIdArr = [self.mRecievedDataDict valueForKey:@"id"];
                self.mFrndMSinceArr = [self.mRecievedDataDict valueForKey:@"memberSince"];
                self.mFrndDescArr = [self.mRecievedDataDict valueForKey:@"description"];

                 [self.mFriendsTable reloadData];
            }    
}

下面是cellForRowAtIndexPath方法。

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell;
        cell = [self.mFriendsTable dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    else{
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
cell.selectionStyle = UITableViewCellSelectionStyleNone;

NSString *imageUrlString = [NSString stringWithFormat:@"http://www.myevent.co/%@",       [self.mFrndImgArr objectAtIndex:indexPath.row]];
[imageUrlString stringByReplacingOccurrencesOfString:@"/" withString:@""];

UILabel *lblEventName = [[UILabel alloc]initWithFrame:CGRectMake(92, 5, 200, 25)];
lblEventName.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
lblEventName.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:lblEventName];

if([copyArr count] >0)
{
    lblEventName.text = [copyArr objectAtIndex:indexPath.row];
}
else{
    lblEventName.text = [self.mFrndNameArr objectAtIndex:indexPath.row];
}

asyncImageView = [[AsyncImageView alloc]initWithFrame:CGRectMake(1, 3, 85, 54)];
[asyncImageView loadImageFromURL:[NSURL URLWithString:imageUrlString]];
[cell.contentView addSubview:asyncImageView];

cell.textLabel.backgroundColor = [UIColor colorWithRed:240.0/255.0 green:240.0/255.0 blue:240.0/255.0 alpha:1.0];
cell.contentView.backgroundColor = [UIColor colorWithRed:240.0/255.0 green:240.0/255.0 blue:240.0/255.0 alpha:1.0];

return cell;
}

提取文本数据,但在滚动表格之前不会提取图像。但是,如果我使用这种方式它也显示图像,但在获取所有数据后切换到下一个视图。

 //   vewDidLoad

[self setImage];

请指导以上,是我在这里使用错误的线程或任何其他方式或方法的方式。

1 个答案:

答案 0 :(得分:0)

由于您要在单独的线程中更新数据,您必须在主线程本身上执行重新加载

而不是

[self.mFriendsTable reloadData];

使用此

[self.mFriendsTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];