【问题标题】:Cannot link my table view controller on storyboard to my code无法将情节提要上的表格视图控制器链接到我的代码
【发布时间】:2014-09-15 23:47:30
【问题描述】:

我正在用 XCode6 编写一个应用程序。目前我有“SelectionTableViewController.h”和“SelectionTableViewController.m”,这样您就可以在选择时添加/删除复选标记。此外,我在情节提要中有一个表格视图控制器 segue,它由前一个表格视图控制器中的静态单元格触发。我在情节提要中设置了触发器,所以我没有为“准备 segue”或任何东西编写代码。 我希望默认检查单元格,所以我做了以下操作:

  • 将视图控制器更改为“SelectionTableViewController”
  • 将故事板上原型单元格的标识符设置为“SelectionCell”
  • 将单元格的背景颜色更改为橙​​色,将附件颜色更改为复选标记

下面是我的 SelectionTableViewController.m:

@implementation SelectionTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class]
           forCellReuseIdentifier:@"SelectionCell"];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
    self.navigationItem.title = @"Select";
    [self.tableView reloadData];
}

- (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 [[[MyStore sharedStore] allCategories] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSString *category = [[[MyStore sharedStore] allCategories] objectAtIndex:section];
    return [[[MyStore sharedStore] allNamesForCategory: category] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath(NSIndexPath*)indexPath
{
    UITableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier:@"SelectionCell"forIndexPath:indexPath];

    // Configure the cell...
    NSString *category = [[[MyStore sharedStore] allCategories] objectAtIndex:indexPath.section];
    NSArray *items = [[MyStore sharedStore] allNamesForCategory: category];

    cell.textLabel.text = [items objectAtIndex:indexPath.row];

    return cell;
}
#pragma mark - Table view delegate

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


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[[MyStore sharedStore] allCategories] objectAtIndex:section];
}

@end

我的程序在模拟器上运行,单元格中列出的项目是正确的。但是,在我进行选择之前,单元格上没有默认复选标记。细胞也不是橙色的。我知道我可以轻松地在代码中设置默认复选标记和背景颜色,但我只想弄清楚如何在界面构建器中执行此操作,因为在设置程序时我会经常处理 UI。

希望有人可以帮助我,因为我对 iOS 编程有点陌生,这是我第一次使用 Storyboard。谢谢!

【问题讨论】:

    标签: objective-c uitableview storyboard ios8 xcode6gm


    【解决方案1】:

    您的实现问题是tableView:didSelectRowAtIndexPath 只会由用户交互触发,因此初始选择不会显示。您还需要在初始化期间让tableView 了解选择状态,否则取消选择将无法按预期运行。

    class ViewController: UITableViewController {
    
        var selectedItem: Int = 5
    
        func updateSelection(selected: Bool, forCell cell: UITableViewCell) {
            cell.accessoryType = selected ? .Checkmark : .None
            // update other cell appearances here..
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        }
    
        override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)
            // Note: happens after the initial tableView.reloadData()
            // Let tableView know who's selected before we appear, so that tableView is aware of the selection state.
            // Doing so will enable tableView to know which cell to deselect after a new selection.
            tableView.selectRowAtIndexPath(NSIndexPath(forItem: selectedItem, inSection: 0), animated: true, scrollPosition: .Middle)
        }
    
        override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return 1
        }
    
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 10
        }
    
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
            cell.textLabel?.text = "Item \(indexPath.item)"
            // Setup the selection appearance during cell creation
            updateSelection(indexPath.item == selectedItem, forCell: cell)
            return cell
        }
    
        override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
            // Keep track of the selection, and update cell appearance
            selectedItem = indexPath.item
            updateSelection(true, forCell: tableView.cellForRowAtIndexPath(indexPath)!)
        }
    
        override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
            // As above
            updateSelection(false, forCell: tableView.cellForRowAtIndexPath(indexPath)!)
        }
    
    }
    

    【讨论】:

    • 其实我自己也想通了。谢谢!
    【解决方案2】:

    我自己想通了!我删除了注册“选择单元”的代码。然后我对 cellForRowAtIndexPath 做了一些小改动

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath(NSIndexPath*)indexPath
    {
        Static NSString *selectionCell = @"SelectionCell"; 
        UITableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier:selectionCell forIndexPath:indexPath];
    
        // Omit...
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-20
      • 2012-01-02
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 2019-11-08
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      相关资源
      最近更新 更多