gpt4 book ai didi

ios - 更新支持 UITableView 的 NSArray 中的值

转载 作者:行者123 更新时间:2023-12-01 16:57:50 26 4
gpt4 key购买 nike

cocoa ,而不是 iOS。我有一组支持 UITableview 的项目。我遍历数组并执行一系列任务,具体来说,我正在使用 NSTask 调用外部脚本可执行文件。在执行任务时,我想更新 UITableView 中的“状态”列。

我更新了与列关联的数组的值,然后调用 [_TableView reloadData],但 UI 中没有更改,直到所有处理完成。

    for (int ix = 0; ix < [_sceneList count]; ix++) {

// update status in array backing the TableView
[[_sceneList objectAtIndex:ix] setRenderStatus:@"Rendering"];
[_tableView reloadData];

// execute command via shell script
[self runScript:@"run_script.bash":appPath:outputFormat:showGUI:threadCnt:resolution:renderFile:outputFile];

[[_sceneList objectAtIndex:ix] setRenderStatus:@"Complete"];
[_tableView reloadData];
}

run_script 的代码:
-(void) runScript:(NSString*)scriptName:(NSString*)appPath:(NSString*)outputFormat:(NSString*)showGUI:(NSString*)threadCnt:(NSString*)resolution:(NSString*)renderFile:(NSString*)outputFile;  
{
NSTask *task;
task = [[NSTask alloc] init];

NSArray *arguments;
NSString* newpath = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath] , scriptName];

[task setLaunchPath: newpath];

arguments = [NSArray arrayWithObjects:appPath, showGUI, renderFile, outputFile, outputFormat, threadCnt, resolution ,nil];

[task setArguments: arguments];

NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
[task setStandardInput:[NSPipe pipe]];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[task launch];

NSData *data;
data = [file readDataToEndOfFile];

NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog (@"script returned:\n%@", string);
}

任何想法为什么 UI 不更新?

我应该使用某种“tableview:updateRow”方法吗?

_麦克风

最佳答案

听起来 runScript 中的某些东西正在同步运行并阻止您的主线程更新 UI。看完this ,我认为你的问题是

data = [file readDataToEndOfFile];

相反,使用以下命令进行异步调用:
[file readInBackgroundAndNotify];

您需要订阅 NotificationCenter 中的通知,可能在您的 init 方法中使用如下内容:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(fileHandleReadCompleted:)
name:NSFileHandleReadCompletionNotification
object:nil];

然后在你的 fileHandleReadCompleted: 你可以使用
data = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem];

检索结果。

关于ios - 更新支持 UITableView 的 NSArray 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9931881/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com