0

何らかの理由で、セクション ヘッダーの検索フィールドを選択してキーボードを表示することができません。また、テーブル ビューのセルに何も表示されませんが、その理由がわかりません。私のコードは以下の通りです:

- (void)viewDidLoad {
  [super viewDidLoad];  

  self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

  self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
  self.tableView.delegate = self;
  [self.view addSubview:self.tableView];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
  [self.searchField resignFirstResponder];

  return NO;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
  return 8;
}

- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  return 60;
}

- (UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section {

  UIView *customHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];

  // Add search field
  UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(40, 20, 250, 50)];
  field.borderStyle = UITextBorderStyleRoundedRect;
  field.delegate = self;
  [customHeader addSubview:field];

  return customHeader;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  NSString *cellIdentifier;

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

    NSLog(@"i am here");

    UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(40, 20, 250, 50)];
    field.borderStyle = UITextBorderStyleRoundedRect;
    field.delegate = self;
    [cell.contentView addSubview:field];
  }

  return cell;
}
4

2 に答える 2

1

そのセクションのヘッダーの高さを指定していないため、機能しないと思います。あなたは次のようなものを持っている必要があります

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  return 50.0;
}

複数のセクションがある場合は、目的のセクションに対してのみこれを行います。

于 2012-06-17T21:38:00.687 に答える
0

nib ファイルやストーリーボードを使用しないのはなぜですか? Appleのドキュメントによると、ストーリーボードまたはnibファイルからuiviewcontrollerのビューをロードできない場合は、オーバーライドする必要があります

-loadView

ビュー階層を設定するメソッド。ビュー管理セクションを読んでください。これにより、おそらく検索フィールドの問題が解決されます。2 番目の問題を解決するには、cellidentifier を設定します

NSString *cellIdentifier = @"Your identifier";

ゼロにはなりえないと思います。私の答えが役立つことを願っています。そうでない場合は、コメントを残してください。

于 2012-06-17T21:19:52.380 に答える