首页 新闻 会员 周边

iOS Apple官方教程 英文版新手入门 中的例子 ToDoList app 按过一个列表项目后右边没有显示打钩

0
悬赏园豆:40 [已解决问题] 解决于 2014-06-16 14:57

编译通过,按下一个列表项目以后,右边不会出现钩,完全没任何变化。

期望的运行结果应该如下图所示:

ToDoListViewController.m:

  1 #import "ToDoListTableViewController.h"
  2 
  3 @interface ToDoListTableViewController ()
  4 
  5 @property NSMutableArray *toDoItems;
  6 
  7 @end
  8 
  9 @implementation ToDoListTableViewController
 10 
 11 - (void)loadInitialData {
 12     ToDoItem *item1 = [[ToDoItem alloc] init];
 13     item1.itemName = @"Buy milk";
 14     [self.toDoItems addObject:item1];
 15     ToDoItem *item2 = [[ToDoItem alloc] init];
 16     item2.itemName = @"Buy eggs";
 17     [self.toDoItems addObject:item2];
 18     ToDoItem *item3 = [[ToDoItem alloc] init];
 19     item3.itemName = @"Read a book";
 20     [self.toDoItems addObject:item3];
 21 
 22 }
 23 
 24 - (IBAction)unwindToList:(UIStoryboardSegue *)segue {
 25     
 26 }
 27 
 28 - (id)initWithStyle:(UITableViewStyle)style
 29 {
 30     self = [super initWithStyle:style];
 31     if (self) {
 32         // Custom initialization
 33     }
 34     return self;
 35 }
 36 
 37 - (void)viewDidLoad
 38 {
 39     [super viewDidLoad];
 40     self.toDoItems = [[NSMutableArray alloc] init];
 41     [self loadInitialData];
 42     
 43     // Uncomment the following line to preserve selection between presentations.
 44     // self.clearsSelectionOnViewWillAppear = NO;
 45     
 46     // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
 47     // self.navigationItem.rightBarButtonItem = self.editButtonItem;
 48 }
 49 
 50 - (void)didReceiveMemoryWarning
 51 {
 52     [super didReceiveMemoryWarning];
 53     // Dispose of any resources that can be recreated.
 54 }
 55 
 56 #pragma mark - Table view data source
 57 
 58 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 59 {
 60     // Return the number of sections.
 61     return 1;
 62 }
 63 
 64 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 65 {
 66     // Return the number of rows in the section.
 67     return [self.toDoItems count];
 68     //NSLog(@"The number of rows is %lu", (unsigned long)[self.toDoItems count]);
 69     
 70 }
 71 
 72 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 73 {
 74     static NSString *CellIndentifier = @"ListPrototypeCell";
 75     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier forIndexPath:indexPath];
 76     
 77     // Configure the cell...
 78     ToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
 79     cell.textLabel.text = toDoItem.itemName;
 80     if (toDoItem.completed) {
 81         cell.accessoryType = UITableViewCellAccessoryCheckmark;
 82     } else {
 83         cell.accessoryType = UITableViewCellAccessoryNone;
 84     }
 85     
 86     return cell;
 87 }
 88 
 89 /*
 90 // Override to support conditional editing of the table view.
 91 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
 92 {
 93     // Return NO if you do not want the specified item to be editable.
 94     return YES;
 95 }
 96 */
 97 
 98 /*
 99 // Override to support editing the table view.
100 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
101 {
102     if (editingStyle == UITableViewCellEditingStyleDelete) {
103         // Delete the row from the data source
104         [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
105     } else if (editingStyle == UITableViewCellEditingStyleInsert) {
106         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
107     }   
108 }
109 */
110 
111 /*
112 // Override to support rearranging the table view.
113 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
114 {
115 }
116 */
117 
118 /*
119 // Override to support conditional rearranging of the table view.
120 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
121 {
122     // Return NO if you do not want the item to be re-orderable.
123     return YES;
124 }
125 */
126 
127 /*
128 #pragma mark - Navigation
129 
130 // In a storyboard-based application, you will often want to do a little preparation before navigation
131 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
132 {
133     // Get the new view controller using [segue destinationViewController].
134     // Pass the selected object to the new view controller.
135 }
136 */
137 
138 #pragma mark - Table view delegate
139 
140 - (void)tableview:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
141 {
142     // Respond to taps but not actually leave the cell selected.
143     [tableView deselectRowAtIndexPath:indexPath animated:NO];
144     // Search for the corresponding ToDoItem in your to DoItems array.
145     ToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
146     // Toggle the completion state of the tapped item.
147     tappedItem.completed = !tappedItem.completed;
148     // Tell the table view to reload the row whose data you just updated.
149     [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
150 }
151 @end

编译没有错误,我作为一个初学者就完全没有办法了。只好问专家!谢谢

现在就是我这部分代码

 1 - (void)tableview:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 2 {
 3     // Respond to taps but not actually leave the cell selected.
 4     [tableView deselectRowAtIndexPath:indexPath animated:NO];
 5     // Search for the corresponding ToDoItem in your to DoItems array.
 6     ToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
 7     // Toggle the completion state of the tapped item.
 8     tappedItem.completed = !tappedItem.completed;
 9     // Tell the table view to reload the row whose data you just updated.
10     [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
11 }
View Code

和这部分代码

 1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 2 {
 3     static NSString *CellIndentifier = @"ListPrototypeCell";
 4     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier forIndexPath:indexPath];
 5     
 6     // Configure the cell...
 7     ToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
 8     cell.textLabel.text = toDoItem.itemName;
 9     if (toDoItem.completed) {
10         cell.accessoryType = UITableViewCellAccessoryCheckmark;
11     } else {
12         cell.accessoryType = UITableViewCellAccessoryNone;
13     }
14     
15     return cell;
16 }
View Code

等于完全没起作用。

 

下面附带其他的源代码,如果有需要的话。

1 #import <UIKit/UIKit.h>
2 #import "ToDoItem.h"
3 
4 @interface ToDoListTableViewController : UITableViewController
5 
6 - (IBAction)unwindToList:(UIStoryboardSegue *)segue;
7 
8 @end
ToDoListTableViewController.h
1 #import <Foundation/Foundation.h>
2 
3 @interface ToDoItem : NSObject
4 
5 @property NSString *itemName;
6 @property BOOL completed;
7 @property (readonly) NSDate *creationDate;
8 
9 @end
ToDoItem.h
1 #import "ToDoItem.h"
2 
3 @implementation ToDoItem
4 
5 @end
ToDoItem.m
1 #import <UIKit/UIKit.h>
2 
3 @interface AddToDoItemViewController : UIViewController
4 
5 @end
AddToDoItemViewController.h
 1 #import "AddToDoItemViewController.h"
 2 
 3 @interface AddToDoItemViewController ()
 4 
 5 @end
 6 
 7 @implementation AddToDoItemViewController
 8 
 9 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
10 {
11     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
12     if (self) {
13         // Custom initialization
14     }
15     return self;
16 }
17 
18 - (void)viewDidLoad
19 {
20     [super viewDidLoad];
21     // Do any additional setup after loading the view.
22 }
23 
24 - (void)didReceiveMemoryWarning
25 {
26     [super didReceiveMemoryWarning];
27     // Dispose of any resources that can be recreated.
28 }
29 
30 /*
31 #pragma mark - Navigation
32 
33 // In a storyboard-based application, you will often want to do a little preparation before navigation
34 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
35 {
36     // Get the new view controller using [segue destinationViewController].
37     // Pass the selected object to the new view controller.
38 }
39 */
40 
41 @end
AddToDoItemViewController.m

其它两个是系统生成的,AppDelegate.h和AppDelegate.m。我就不附带了。

问题补充:

实际上就是,我这几行代码应该就可以让被我点击的那一行最后右侧带钩了。

1     if (toDoItem.completed) {
2         cell.accessoryType = UITableViewCellAccessoryCheckmark;
3     } else {
4         cell.accessoryType = UITableViewCellAccessoryNone;
5     }

但实际上没有,不知道为什么,怎么debug?想看相应cell的accessoryType属性现在的值应该怎么看?

 

这些对我来说都太深了,这才是我的第一个程序。还请大家帮忙!

 

参看http://www.cocoachina.com/bbs/read.php?tid=49240&keyword=cell%7CaccessoryType,

通过 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; ,他的就在右侧显示了,我的就没有。

锅仔排骨的主页 锅仔排骨 | 初学一级 | 园豆:166
提问于:2014-05-12 20:25
< >
分享
最佳答案
1

                  

 storyboard Table view上Selection 那一项要用single selection 

收获园豆:40
Angel Demon | 菜鸟二级 |园豆:242 | 2014-06-16 14:52
其他回答(2)
0

你是使用storyBoard搭的页面吗?设置代理了没?你可以在代理方法里面先打个断点,或者输出下语句试试

撩课-Sz | 园豆:330 (菜鸟二级) | 2014-05-28 17:08

您好,我是用Storyboard搭的。

delegate不会设。我用代码设置的不能用:

    

    self.tableView.delegate=self;

    self.tableView.dataSource=self;

 

Storyboard怎么设代理我不会用。

非常感谢!

支持(0) 反对(0) 锅仔排骨 | 园豆:166 (初学一级) | 2014-06-12 17:35

@锅仔排骨: 你在代理方法里面做个测试,先看看有没有执行到。你要直接使用storyboard拖得tableviewcontroller的话,应该直接就设置代理了,或者你看看tableviewcontroller类名改了没,具体没看到代码也不好判断。

支持(0) 反对(0) 撩课-Sz | 园豆:330 (菜鸟二级) | 2014-06-12 18:11
0

iOS Apple官方教程 英文版新手入门 这个是storyboard版本的?

Jackson_Yi | 园豆:202 (菜鸟二级) | 2016-07-03 18:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册