0
我想使用推視圖控制器立即切換到下一個視圖,所以使用下面的方法從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;
}
提取文本數據,但在滾動表格之前不會提取圖像。但是,如果我使用這種方式,它也顯示圖像,但在所有dat被提取後切換到下一個視圖。
// vewDidLoad
[self setImage];
請指導以上,是我使用線程錯誤的方式或任何其他方式或方法需要在這裏使用。