UITableViewCell detailTextLabel在为其分配新值时不会更新其文本

时间:2017-12-26 01:07:46

标签: ios objective-c uitableview uiviewcontroller uikit

我一直在尝试使用静态表视图作为我的应用中警报功能的设置/选项视图。我能够传递和更新各种ViewControllers和更新值的数据。

永远MPMediaPickerController会愉快地传回数据,它甚至会在保存值后使用正确的值更新委托,但在保存alarmObject之前它不会更新detailTextLabel。我可以在我的日志中看到,当ViewController被解除时,对象甚至会以正确的值保存。唯一奇怪的是,有问题的VC是另一个VC的子VC,但我不认为这是问题。

如果有帮助,所有VC也是UINavigationController的一部分,但MediaPicker除外。

我试图重新加载数据,在主线程上执行更改,延迟主线程让所有更改执行,重新加载mediaCell所在的特定行,在单元格上调用needsDisplay和needsLayout以及layoutIfNeeded,没有作品。任何建议都非常受欢迎,因为我过去几天一直在反对这一点并且感到沮丧。

#import "AddAlarmTableViewController.h"
#import "ToneTableViewController.h"

@interface AddAlarmTableViewController ()

@property (weak, nonatomic) IBOutlet UITableViewCell *mediaCell;

@end

@implementation AddAlarmTableViewController


@synthesize previousLabel, previousSoundAsset, previousRepeatArray,alarmDelegate,mediaCell;

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:NO];
    if(previousSoundAsset != nil) {
        mediaCell.detailTextLabel.text = previousSoundAsset;
    } else {
        mediaCell.detailTextLabel.text = @" ";
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if([cell.reuseIdentifier isEqualToString:@"mediaCell"]) {
        UIAlertController *selectTypeAlertController = [UIAlertController alertControllerWithTitle:@"Select Media From" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        //Creating actions
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *selectFromMusicAction = [UIAlertAction actionWithTitle:@"Music" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
            [self launchMediaPickerController];
        }];
        UIAlertAction *selectFromToneAction = [UIAlertAction actionWithTitle:@"Tone" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
            [self launchTonePickerController];
        }];
        //adding them to uialertcontroller;
        [selectTypeAlertController addAction:cancelAction];
        [selectTypeAlertController addAction:selectFromMusicAction];
        [selectTypeAlertController addAction:selectFromToneAction];
        [self presentViewController:selectTypeAlertController animated:true completion:nil];

    }
}
- (void)launchTonePickerController {
    ToneTableViewController *toneVC = [[ToneTableViewController alloc] init];
    toneVC.alarmDelegate = alarmDelegate;
    [self.navigationController pushViewController:toneVC animated:true];

}
- (void)launchMediaPickerController {
    NSLog(@"Launching MPMediaPickerController");
    MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
    mediaPicker.delegate = self;
    mediaPicker.allowsPickingMultipleItems = NO;

    [self presentViewController:mediaPicker animated:true completion:nil];
}

//MPMediaPickerDelegate methods
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
    [self dismissViewControllerAnimated:true completion:^ {
        NSLog(@"MPMediaPickerController dismissed");
    }];
}
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
    MPMediaItem *selectedTrack = [[mediaItemCollection items] objectAtIndex:0];
    alarmDelegate.soundAsset = selectedTrack.title;
    mediaCell.detailTextLabel.text = alarmDelegate.soundAsset;
    alarmDelegate.appTones = [[NSNumber alloc] initWithBool:NO];
    [self dismissViewControllerAnimated:true completion:^ {
        NSLog(@"MPMediaPickerController dismissed with %@", alarmDelegate.soundAsset);
        dispatch_async(dispatch_get_main_queue(), ^ {
            [self.tableView reloadData];
        });
    }];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"repeatSegue"]) {
        DayTableViewController *daysViewController = segue.destinationViewController;
        daysViewController.alarmDelegate = alarmDelegate;
        if(previousRepeatArray != nil) {
            daysViewController.previousSelection = previousRepeatArray;
        }
    }
    else if([segue.identifier isEqualToString:@"labelSegue"]) {
        LabelViewController *labelViewController = segue.destinationViewController;
        labelViewController.alarmDelegate = alarmDelegate;
        if(previousLabel != nil) {
            labelViewController.previousLabel = previousLabel;
        }
    }
}
@end

2 个答案:

答案 0 :(得分:0)

请检查previousSoundAsset的值。它可能是空字符串(不是零)。

if(previousSoundAsset != nil || ![previousSoundAsset isEquals:@""]) {
    mediaCell.detailTextLabel.text = previousSoundAsset;
} else {
    mediaCell.detailTextLabel.text = @" ";
}

答案 1 :(得分:0)

好的,正如Apple的文档所示,如果您正在使用故事板和静态表视图,建议只需将IBOutlet附加到相关项目并使用IBOutlet直接设置上述属性item,在本例中是detailTextLabel。我仍然不知道为什么我不能通过didSelectRow UITableViewDelegate中返回给我的单元格直接更改detailTextLabel但是你去了。

我怀疑当我尝试在主线程上的GCD块中时,我没有尝试执行此操作,因此也是如此。请注意,实际延迟发生的时间有可察觉的延迟,所以请注意这一点。