我目前正在开发一个应用程序,其行为像Messages.app。 MasterViewController是加载联系人名称,时间和最近消息片段的表格的主视图。当你点击一个特定的单元格时,它会滑动到DetailViewController,它将加载我发送给联系人的消息并显示最新的完整消息。点击后退按钮返回到MasterViewController。点击rightBarButtonItem打开一个ComposeViewController(模式),用户可以在其中编写消息给特定的联系人。此应用程序与默认Messages.app的区别在于它在发送消息之前有一个延迟计时器。 ComposeViewController有一个用于输入消息的文本框,用于选择联系人的按钮,用于选择时间延迟的按钮,用于发送的按钮,用于取消定时器的按钮以及用于关闭ModalViewController的按钮。如何使用自定义tableviewcells正确添加和删除对象到tableview?
我删除了完全发送实际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.
}
}
去哪里是最后一段代码?这将创建主视图控制器的新实例,它不会影响当前的主表。 – jrturton 2011-12-15 11:00:03
位于模型视图控制器的实现中,您可以将文本放置在文本字段中,然后点击按钮以关闭模式视图控制器。 – jaytrixz 2011-12-16 03:06:07