私は現在、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.
}
}