我想更新数据tableView
并将新数据作为新的初始行插入到UITableView
. 我有两个数组:FileName
数组包含我使用从服务器获取的数据ParseXM
和一个临时数组,用于从FileNameArray
. 我写了一个函数UpdateArray
来获取新数据,我将数据复制FileNameArray
到临时数组中ViewDidLoad()
,然后调用UpdateArray
;首先我删除所有条目,然后FileNameArray
向服务器发送一个请求ParseXML
,然后我比较FileNameArray
和临时数组,如果FileName arr > temp arr
:删除所有条目并从arrtemp arr
复制到,然后重新加载数据但不显示第一行中的新数据( s)。FileName
temp arr
UITableview
tableView
cellForRowAtIndexPath():
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray* FileNamereversed = [[FileCompletedArray reverseObjectEnumerator] allObjects];
NSArray* UploadTimereversed = [[UploadTimeArray reverseObjectEnumerator] allObjects];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *FileNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
FileNameLabel.backgroundColor = [UIColor clearColor];
FileNameLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
FileNameLabel.font = [UIFont boldSystemFontOfSize:16];
FileNameLabel.textColor = [UIColor blackColor];
NSLog(@"Reseversed TEMP array %@",FileNamereversed);
FileNameLabel.text =[FileNamereversed objectAtIndex:indexPath.row];
[cell.contentView addSubview: FileNameLabel];
[FileNameLabel release];
UILabel *UploadTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 25)];
UploadTimeLabel.backgroundColor = [UIColor clearColor];
UploadTimeLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
UploadTimeLabel.textColor = [UIColor grayColor];
UploadTimeLabel.text = [UploadTimereversed objectAtIndex:indexPath.row];
[cell.contentView addSubview: UploadTimeLabel];
[UploadTimeLabel release];
UILabel *CompleteLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 12, 170, 25)];
CompleteLabel.backgroundColor = [UIColor clearColor];
CompleteLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
CompleteLabel.textColor = [UIColor darkGrayColor];
CompleteLabel.text =@"Completed";
CompleteLabel.textAlignment = NSTextAlignmentRight;
[cell.contentView addSubview: CompleteLabel];
[CompleteLabel release];
}
//[temp removeAllObjects];
// temp = [FileCompletedArray mutableCopy];
return cell;
}
更新数组():
-(void)updateArray{
[NSThread sleepForTimeInterval:4.0];
[FileCompletedArray removeAllObjects];
...
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success");
NSString * parsexmlinput = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Response in Loop CompleteView: %@", parsexmlinput); //000400010001
// dispatch_async(dispatch_get_main_queue(), ^{
[self parseXMLFile:parsexmlinput];
NSLog(@"File Completed array: %@", FileCompletedArray);
NSLog(@"File Temp out array: %@", temp);
NSLog(@"File Completed count: %lu",(unsigned long)[ FileCompletedArray count]);
NSLog(@"File Temp out count: %lu", (unsigned long)[temp count]);
// NSLog(@"State: %@", state);
if([FileCompletedArray count ] != [temp count]) // compare 2 array
{
[temp removeallObjects];
temp = [FileCompletedArray mutableCopy];
[_tableView reloadData];
}
else
{
NSLog(@"2 array equal");
}
}
当我打电话时UpdateArray
,虽然服务器有新数据,但两个数组彼此相等,所以我tableView
不会更新。你能告诉我解决方案吗?提前致谢。
编者的话:原来的措辞几乎是难以理解的——在更正时我可能遗漏或误读了某些方面。强烈建议原作者进行校对。