【问题标题】:Tapping the cell selections does not change the cell accessory to a checkmark点击单元格选择不会将单元格附件更改为复选标记
【发布时间】:2014-09-04 06:41:03
【问题描述】:

有人可以帮助我吗?我一直在关注 Apple 的官方 Objective C 教程(让我们创建待办事项列表的教程)。

我卡在点击单元格项目显示/删除复选标记的部分。它没有发生。

如果有人可以看一下,这是我的实现文件?

我真的尽了最大的努力来解决我出错的地方。我发誓,在发帖之前,我已尽力自己弄清楚所有事情。请,任何帮助将不胜感激!

谢谢!

编辑:所以我在 didSelectRowAtIndexPath 方法中放置了一个断点。当我点击一个表格项目时它没有被触发,所以当我点击项目时它不会被调用,对吧?下一步我该往哪里看?

//
//  ToDoListTableViewController.m
//  ToDoList
//
//  Created by Kevin Zagala on 9/1/14.
//  Copyright (c) 2014 Kevin Zagala. All rights reserved.
//

#import "ToDoListTableViewController.h"
#import "ToDoItem.h"


@interface ToDoListTableViewController ()

@property NSMutableArray *toDoItems;

@end

@implementation ToDoListTableViewController

- (IBAction)unwindToList:(UIStoryboardSegue *)segue
{
}

- (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];

}

- (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 mark - 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];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ListPrototypeCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    ToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];

    cell.textLabel.text = toDoItem.itemName;

    if (toDoItem.completed) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

#pragma mark - Table view delegate
- (void)tableview:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    ToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
    tappedItem.completed = !tappedItem.completed;
    [tableView reloadRowsAtIndexPaths:@[indexPath]
                     withRowAnimation:UITableViewRowAnimationNone];

}

@end

【问题讨论】:

  • 尝试重新加载整个 tableview 而不是重新加载 didSelectRowAtIndexPath 中的单行
  • @Shan 我该怎么做?如果你不知道的话,我对目标 c 编程还是很陌生。 :)
  • 我发布了一个答案检查它
  • 我已经更新了我的代码一次检查它,它对我有用..
  • 将 NSLog 添加到单元格中以检查 tappedItem.completed 的值

标签: ios objective-c uitableview


【解决方案1】:

试试这个..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ListPrototypeCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    ToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];

    cell.textLabel.text = toDoItem.itemName;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryNone)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}

【讨论】:

  • 谢谢,但这没有帮助。将 if/else 语句更改为您建议的内容后,我现在看到的是显示复选标记,但点击时不会离开。我可以在界面生成器或其他项目文件之一中更改什么可以帮助解决这个问题吗?
  • 还是不行。嗯...在旁注中,当我调用初始加载函数并将 .completed 设置为 TRUE 的项目时,会显示一个复选标记。但是,它仍然没有响应点击...如,如果点击它不会消失,如果不显示它会重新出现。
  • 在我发布的代码中,细胞附件类型和.completed之间没有关系
【解决方案2】:

尝试重新加载整个 tableview,

你正在做的应该工作,我通过放置一个具有布尔值的虚拟对象来测试你的代码,它对我有用,

注意重新加载整个表格视图和重新加载选定的单元格都可以,只有你设置了正确的布尔值

 - (void)tableview:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
   [tableView deselectRowAtIndexPath:indexPath animated:NO];
   ToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
   tappedItem.completed = !tappedItem.completed;
//   [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; //comment this
    [tableView reloadData]; //add this 
}

【讨论】:

  • 嗨,山,这对我不起作用。不过谢谢。我认为它是我设置不正确的其他东西......呃......再次感谢。
  • cellForRowAtIndexPath处设置一个断点并检查toDoItem的完成标志是否设置,如果你设置了toDoItem的完成的布尔值,你正在做的应该工作
  • 第一次加载时会触发断点,但单击项目时不会触发
猜你喜欢
  • 2016-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多