0

我目前正在开发一个行为类似于 Messages.app 的应用程序。MasterViewController 是加载联系人姓名、时间和最新消息片段的表格的主视图。当您点击特定单元格时,它会滑动到 DetailViewController,它会在其中加载我发送给联系人的消息以及最新的完整消息。点击后退按钮返回到 MasterViewController。点击 rightBarButtonItem 会打开一个 ComposeViewController(模式),用户可以在其中向特定联系人撰写消息。这个应用程序与默认 Messages.app 的不同之处在于它在发送消息之前有一个延迟计时器。ComposeViewController 有一个用于输入消息的文本字段,用于选择联系人的按钮,用于选择时间延迟的按钮,用于发送的按钮,用于取消计时器的按钮,

我完全删除了发送实际 SMS 消息的能力。我只是向用户展示了一个警报视图,告诉他/她消息已发送,以及他/她是否想要撰写新消息。点击取消将关闭 ModalViewController 并返回到 MasterViewController。

问题是,我不能让行出现在表格上,也不能在表格中添加和删除单元格。

这是我的 MasterViewController 的 viewDidLoad 中的一些代码:

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

// Delete button to delete messages
UIBarButtonItem *deleteBarButtonItem = [[UIBarButtonItem alloc] 
                                      initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
                                      target:self 
                                      action:@selector(deleteText)];
self.navigationItem.leftBarButtonItem = deleteBarButtonItem;


// Compose button to go to compose messages
UIBarButtonItem *composeBarButtonItem = [[UIBarButtonItem alloc] 
                                         initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
                                         target:self 
                                         action:@selector(composeText)];
self.navigationItem.rightBarButtonItem = composeBarButtonItem;

[deleteBarButtonItem release];
[composeBarButtonItem release];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *message = [defaults objectForKey:kMessageText];
NSString *contactname = [defaults objectForKey:kContactNameText];
NSString *timestamp = [defaults objectForKey:kTimeStampText];

[messageDetails initWithObjectsAndKeys:contactname, kContactNameKey, message, kContactMsgKey, timestamp, kContactTimeKey, nil];

NSMutableArray *messageInfo = [[NSMutableArray alloc] initWithObjects:messageDetails, nil];

self.messagesList = messageInfo;

[messageInfo release];

[super viewDidLoad];

这是 cellForRowAtIndexPath 中的代码:

CustomCellViewController *customCell = (CustomCellViewController *)[tableView dequeueReusableCellWithIdentifier:@"CustomCellViewController"];

if (customCell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellViewController"
                                                 owner:self 
                                               options:nil];
    for (id oneObject in nib) if ([oneObject isKindOfClass:[CustomCellViewController class]])
        customCell = (CustomCellViewController *)oneObject;
}

NSUInteger row = [indexPath row];
NSDictionary *messages = [self.messagesList objectAtIndex:row];

customCell.nameLabel.text = [messages objectForKey:kContactNameKey];
customCell.nameLabel.textColor = [UIColor whiteColor];
customCell.messageLabel.text = [messages objectForKey:kContactMsgKey];
customCell.messageLabel.textColor = [UIColor lightGrayColor];
customCell.timeLabel.text = [messages objectForKey:kContactTimeKey];
customCell.timeLabel.textColor = [UIColor blueColor];

customCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return customCell;

这是删除单元格的代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
    // Delete the row from the data source.        
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade];

    [messagesList removeObjectAtIndex:indexPath.row];
    [self.tableView reloadData];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
4

1 回答 1

0

您在其中创建主视图控制器的新实例的最终代码片段是问题所在。

这不是您正在寻找的视图控制器。由于您已经以模态方式呈现了详细视图控制器,因此您可以通过详细控制器的属性访问主parentViewController控制器:

MasterViewController *master = (MasterViewController*)self.parentViewController;

在这种情况下通常使用的其他设计模式:

  • 在主控制器中创建新对象并在表中插入行,然后将新对象传递给详细控制器进行更新
  • 为您的主控制器遵循的细节控制器创建一个委托协议。推送时将详细控制器委托给主控制器

后者几乎是您为所有实际目的所做的事情,除了您的详细控制器对主控制器的了解超过它所需的信息(即您正在导入整个主 .h 文件,而不仅仅是知道它符合协议)。

关于您的数据结构,我不明白您希望在这里有多行 - 您将一条消息存储在用户默认值中,然后使用该消息创建一个数组。我知道您最终不打算使用默认值来存储它,但我希望看到一个字典数组存储在一个键下的默认值中,然后每个字典代表表中的一行,以及各种详细信息根据您的消息、联系人姓名键等存储为字典中的字符串。

您必须制作从默认值返回的数组的可变版本,因为它总是返回一个不可变数组。

在您的 cellForRow... 方法中,您将从数组中获取适当的字典并从中填充单元格。

添加新行时,您将创建一个新字典以传递给您的详细信息控制器。

删除一行时,您将从数组中删除相关字典。

于 2011-12-16T07:09:17.693 回答