2014-09-15 2 views
0

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 프로그래밍을 처음 접했을 때 나를 도울 수 있기를 바랍니다. 스토리 보드를 사용한 적이 처음입니다. 감사!

답변

0

나는 그것을 스스로 알아 냈습니다! 나는 "Selection Cell"을 등록한 코드를 삭제했다. 그런 다음 cellForRowAtIndexPath에 약간의 변경을했습니다.

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

    // Omit... 
} 
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)!) 
    } 

} 
+0

사실 나는 그것을 직접 알아 냈습니다. 감사! –