Objective c 无法将序列图像板上的“我的表视图”控制器链接到我的代码
我正在用XCode6编写一个应用程序。目前我有“SelectionTableViewController.h”和“SelectionTableViewController.m”,这样您就可以在select上添加/删除复选标记。另外,我在故事板中有一个表视图控制器segue,它由上一个表视图控制器中的静态单元触发。我在故事板中设置了触发器,所以我没有为“准备segue”或任何东西编写代码。 我希望默认情况下选中单元格,因此我已执行以下操作:Objective c 无法将序列图像板上的“我的表视图”控制器链接到我的代码,objective-c,uitableview,storyboard,ios8,xcode6gm,Objective C,Uitableview,Storyboard,Ios8,Xcode6gm,我正在用XCode6编写一个应用程序。目前我有“SelectionTableViewController.h”和“SelectionTableViewController.m”,这样您就可以在select上添加/删除复选标记。另外,我在故事板中有一个表视图控制器segue,它由上一个表视图控制器中的静态单元触发。我在故事板中设置了触发器,所以我没有为“准备segue”或任何东西编写代码。 我希望默认情况下选中单元格,因此我已执行以下操作: 将视图控制器更改为“SelectionTableVie
- 将视图控制器更改为“SelectionTableViewController”
- 将我的故事板上原型单元的标识符设置为“SelectionCell”
- 将单元格的背景色更改为橙色,将附件更改为选中标记
@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编程有点陌生,这是我第一次使用故事板。谢谢 实现的问题是
tableView:didselectRowatinedexPath
只会由用户交互触发,因此不会显示初始选择。您还需要在初始化期间使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)!)
}
}
我自己也弄明白了!我删除了注册“选择单元”的代码。然后我对cellForRowAtIndexPath做了一些小改动
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath(NSIndexPath*)indexPath
{
Static NSString *selectionCell = @"SelectionCell";
UITableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier:selectionCell forIndexPath:indexPath];
// Omit...
}
事实上,我自己已经弄明白了。谢谢