Cocoa练习01:一个简单的Todo list程序

时间:2023-11-23 22:21:26

写一个简单的todo list程序,界面如下图:

Cocoa练习01:一个简单的Todo list程序

在TextField区域输入文字,点击Add按钮会将文字显示在下面的TableView列表中。TableView列表有2列,第一列是文字的输入时间;第二列是文字内容本身。同时TableView的第二列是可以实时手动编辑修改的,修改后自动会修正对应第一列的时间:

Cocoa练习01:一个简单的Todo list程序

1.在AppDelegate类的接口中首先绑定2个outlet和1个action:

- (IBAction)add:(id)sender;
@property (weak) IBOutlet NSTableView *tableView;
@property (weak) IBOutlet NSTextField *textField;

2.绑定TableView的delegate和dataSource为AppDelegate本身:

Cocoa练习01:一个简单的Todo list程序

3.最后代码补全如下:

AppDelegate.h

//
//  AppDelegate.h
//  TodoList
//
//  Created by kinds on 15/6/28.
//  Copyright (c) 2015年 hopy. All rights reserved.
//

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>
- (IBAction)add:(id)sender;
@property (weak) IBOutlet NSTableView *tableView;

@property (weak) IBOutlet NSTextField *textField;

@end

AppDelegate.m

//
//  AppDelegate.m
//  TodoList
//
//  Created by kinds on 15/6/28.
//  Copyright (c) 2015年 hopy. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate (){
    NSMutableArray *todo_list;
    //NSMutableDictionary *todo_list_dict;
}

@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate

-(id)init{
    self = [super init];
    if(self){
        todo_list = [NSMutableArray array];
        //todo_list_dict = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
}

-(id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tableColumn
           row:(NSInteger)row{
    //NSLog(@"%s:%@",__func__,[tableColumn identifier]);
    NSString *colID = [tableColumn identifier];
    NSDictionary *dict = [todo_list objectAtIndex:row];
    return [dict objectForKey:colID];
    //return [todo_list objectAtIndex:row];
}

-(void)tableView:(NSTableView*)tv setObjectValue:(id)obj forTableColumn:(NSTableColumn*)tableColumn
             row:(NSInteger)row{
    NSDictionary *dict = @{@"date":short_now_string(),@"note":obj};
    [todo_list replaceObjectAtIndex:row withObject:dict];
    [_tableView reloadData];
}

-(NSInteger)numberOfRowsInTableView:(NSTableView *)tv{
    return (NSInteger)[todo_list count];
}

NSString* short_now_string(void){
    NSDateFormatter *ft = [NSDateFormatter new];
    ft.dateFormat = @"Y-M-d H:m:s z";
    return [ft stringFromDate:[NSDate new]];
}

- (IBAction)add:(id)sender {
    NSString *str = [_textField stringValue];
    if([str length] == 0)
        return;
    //[todo_list addObject:str];
    //NSDateFormatter *ft = [NSDateFormatter new];
    //ft.dateFormat = @"Y-M-d H:m:s z";
    NSDictionary *dict = @{@"date":short_now_string(),@"note":str};
    [todo_list addObject:dict];
    [_tableView reloadData];
    [_textField setStringValue:@""];
}
@end