我试图通过按下位于UITableViewCell
上的按钮在后台运行线程。
现在,我有一个方法,该方法在每次按下按钮时都会被调用,它会更改图标(从播放到暂停),并且还会执行其他操作。
如果线程成功完成(尽管您可以使用单元格上的按钮将其停止),则此按钮应从暂停图标过渡到播放图标,以表明线程已正确完成。
我尝试将单元格本身传递到另一个线程中,然后编辑dispatch_sync
内的单元格按钮。这里的xCode不会引发任何错误,但是按钮图标不会更改。
代码如下:
-(void)thread:(NSDictionary*)taskAssets{
@autoreleasepool {
// Unpacking data
TaskObject *taskData = taskAssets[@"taskData"];
UILabel *statusLabel = taskAssets[@"statusLabel"];
CustomTaskCell *cell = taskAssets[@"cell"];
printf("Wow, this is the thread data: %s\n", [taskData.getTaskName UTF8String]);
sleep(10);
printf("Wow, the thread is done\n");
dispatch_sync(dispatch_get_main_queue(), ^{
[cell setSelected:FALSE];
[cell.button setImage:[UIImage imageNamed:@"playIcon"] forState:UIControlStateNormal];
statusLabel.text = @"Status: Finished";
});
}
}
有什么办法可以做到这一点?
这是图标更改的地方(是发送方是按钮)
- (IBAction)runTask:(id)sender {
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:@"playIcon"] forState:UIControlStateNormal];
[sender setSelected:NO];
[self updateStatus:@"Stopped"];
} else {
[sender setImage:[UIImage imageNamed:@"pauseIcon"] forState:UIControlStateSelected];
[sender setSelected:YES];
[self updateStatus:@"Running..."];
[self launchThread]; // This is where the thread starts...
}
}
答案 0 :(得分:-1)
我可以看到您打给
[self launchThread];
发件人/信元如何最终出现在您在
-(void)thread:(NSDictionary*)taskAssets
上的词典中
我不完全了解@autoreleasepool
的功能,但是也许您可以尝试以下方法:
-(void)thread:(NSDictionary*)taskAssets{
@autoreleasepool {
// Unpacking data
__block TaskObject *taskData = taskAssets[@"taskData"];
__block UILabel *statusLabel = taskAssets[@"statusLabel"];
__block CustomTaskCell *cell = taskAssets[@"cell"];
printf("Wow, this is the thread data: %s\n", [taskData.getTaskName UTF8String]);
sleep(10);
printf("Wow, the thread is done\n");
dispatch_sync(dispatch_get_main_queue(), ^{
[cell setSelected:FALSE];
[cell.button setImage:[UIImage imageNamed:@"playIcon"] forState:UIControlStateNormal];
statusLabel.text = @"Status: Finished";
});
}
}