编译通过,按下一个列表项目以后,右边不会出现钩,完全没任何变化。
期望的运行结果应该如下图所示:与之相关的源文件为 ToDoListViewController.m:
//
// ToDoListTableViewController.m
// ToDoList
//
// Created by Shajide on 10/05/2014.
// Copyright (c) 2014 Shajide. All rights reserved.
//#import "ToDoListTableViewController.h"@interface ToDoListTableViewController ()@property NSMutableArray *toDoItems;@end@implementation ToDoListTableViewController- (void)loadInitialData {
ToDoItem *item1 = [[ToDoItem alloc] init];
item1.itemName = @"Buy milk";
[self.toDoItems addObject:item1];
ToDoItem *item2 = [[ToDoItem alloc] init];
item2.itemName = @"Buy eggs";
[self.toDoItems addObject:item2];
ToDoItem *item3 = [[ToDoItem alloc] init];
item3.itemName = @"Read a book";
[self.toDoItems addObject:item3];}- (IBAction)unwindToList:(UIStoryboardSegue *)segue {
}- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}- (void)viewDidLoad
{
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc] init];
[self loadInitialData];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}#pragma - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.toDoItems count];
//NSLog(@"The number of rows is %lu", (unsigned long)[self.toDoItems count]);
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIndentifier = @"ListPrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier forIndexPath:indexPath];
// Configure the cell...
ToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
if (toDoItem.completed) {
cell.accessoryType = UITableViewCellAccessoryCheck;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*//*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} 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
}
}
*//*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*//*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*//*
#pragma - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/#pragma - Table view delegate- (void)tableview:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Respond to taps but not actually leave the cell selected.
[tableView deselectRowAtIndexPath:indexPath animated:NO];
// Search for the corresponding ToDoItem in your to DoItems array.
ToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
// Toggle the completion state of the tapped item.
tappedItem.completed = !tappedItem.completed;
// Tell the table view to reload the row whose data you just updated.
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
@end因为我完全是新手,也不知道是不是其它文件出了问题。所以也一并附上:
ToDoListTableViewController.h
#import <UIKit/UIKit.h>
#import "ToDoItem.h"@interface ToDoListTableViewController : UITableViewController- (IBAction)unwindToList:(UIStoryboardSegue *)segue;@endAddToDoItemViewController.h
#import <UIKit/UIKit.h>@interface AddToDoItemViewController : UIViewController@end
AddToDoItemViewController.m
//
// AddToDoItemViewController.m
// ToDoList
//
// Created by Shajide on 10/05/2014.
// Copyright (c) 2014 Shajide. All rights reserved.
//#import "AddToDoItemViewController.h"@interface AddToDoItemViewController ()@end@implementation AddToDoItemViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}/*
#pragma - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/@endToDoItem.h
#import <Foundation/Foundation.h>@interface ToDoItem : NSObject@property NSString *itemName;
@property BOOL completed;
@property (readonly) NSDate *creationDate;@end
ToDoItem.m
#import "ToDoItem.h"@implementation ToDoItem@end
其它两个是系统生成的,AppDelegate.h和AppDelegate.m。我就不附带了。
期望的运行结果应该如下图所示:与之相关的源文件为 ToDoListViewController.m:
//
// ToDoListTableViewController.m
// ToDoList
//
// Created by Shajide on 10/05/2014.
// Copyright (c) 2014 Shajide. All rights reserved.
//#import "ToDoListTableViewController.h"@interface ToDoListTableViewController ()@property NSMutableArray *toDoItems;@end@implementation ToDoListTableViewController- (void)loadInitialData {
ToDoItem *item1 = [[ToDoItem alloc] init];
item1.itemName = @"Buy milk";
[self.toDoItems addObject:item1];
ToDoItem *item2 = [[ToDoItem alloc] init];
item2.itemName = @"Buy eggs";
[self.toDoItems addObject:item2];
ToDoItem *item3 = [[ToDoItem alloc] init];
item3.itemName = @"Read a book";
[self.toDoItems addObject:item3];}- (IBAction)unwindToList:(UIStoryboardSegue *)segue {
}- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}- (void)viewDidLoad
{
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc] init];
[self loadInitialData];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}#pragma - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.toDoItems count];
//NSLog(@"The number of rows is %lu", (unsigned long)[self.toDoItems count]);
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIndentifier = @"ListPrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier forIndexPath:indexPath];
// Configure the cell...
ToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
if (toDoItem.completed) {
cell.accessoryType = UITableViewCellAccessoryCheck;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*//*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} 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
}
}
*//*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*//*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*//*
#pragma - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/#pragma - Table view delegate- (void)tableview:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Respond to taps but not actually leave the cell selected.
[tableView deselectRowAtIndexPath:indexPath animated:NO];
// Search for the corresponding ToDoItem in your to DoItems array.
ToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
// Toggle the completion state of the tapped item.
tappedItem.completed = !tappedItem.completed;
// Tell the table view to reload the row whose data you just updated.
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
@end因为我完全是新手,也不知道是不是其它文件出了问题。所以也一并附上:
ToDoListTableViewController.h
#import <UIKit/UIKit.h>
#import "ToDoItem.h"@interface ToDoListTableViewController : UITableViewController- (IBAction)unwindToList:(UIStoryboardSegue *)segue;@endAddToDoItemViewController.h
#import <UIKit/UIKit.h>@interface AddToDoItemViewController : UIViewController@end
AddToDoItemViewController.m
//
// AddToDoItemViewController.m
// ToDoList
//
// Created by Shajide on 10/05/2014.
// Copyright (c) 2014 Shajide. All rights reserved.
//#import "AddToDoItemViewController.h"@interface AddToDoItemViewController ()@end@implementation AddToDoItemViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}/*
#pragma - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/@endToDoItem.h
#import <Foundation/Foundation.h>@interface ToDoItem : NSObject@property NSString *itemName;
@property BOOL completed;
@property (readonly) NSDate *creationDate;@end
ToDoItem.m
#import "ToDoItem.h"@implementation ToDoItem@end
其它两个是系统生成的,AppDelegate.h和AppDelegate.m。我就不附带了。
if (toDoItem.completed) {
cell.accessoryType = UITableViewCellAccessoryCheck;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
但实际上没有,不知道为什么,怎么debug?想看相应cell的accessoryType属性现在的值应该怎么看?
这些对我来说都太深了,这才是我的第一个程序。还请大家帮忙!