0

myArrayからオブジェクトの数を返そうとしていますが、値に応じて、tableViewのheaderViewを変更します。アプリは次のように機能しています。ユーザーはオブジェクトを検索します。オブジェクトが存在する場合、オブジェクトはtableViewに表示されます。ユーザーが存在しないオブジェクトを検索すると、tableViewには何も表示されません。とても簡単..

ユーザーが存在しないオブジェクトを検索しても、配列が何らかの方法で値を格納しているように見え、tableView / arrayはもうnilではありませんか?ただし、tableViewには何も表示されません。私は何が間違っているのですか?searchResultsは私のtableViewの配列です。

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.searchResults count];
}

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

 if (cell == nil)
 {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Task *task = [self.searchResults objectAtIndex:indexPath.row];

UILabel *nameLabel = (UILabel *)[cell viewWithTag:1];
UILabel *bookLabel = (UILabel *)[cell viewWithTag:2];
UILabel *chapterLabel = (UILabel *)[cell viewWithTag:3];

[nameLabel setText:task.name];
[bookLabel setText:task.book.name];
[chapterLabel setText:task.chapter.name];


return cell;
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];

if (self.searchResults == nil){

    headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"lista2.png"]];

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(5, 3, tableView.bounds.size.width - 5, 18)] autorelease];
    label.text = @"Information";
    label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor clearColor];
    [headerView addSubview:label];
  }

  else if (self.searchResults != nil){

    headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"lista2.png"]];

    UILabel *label2 = [[[UILabel alloc] initWithFrame:CGRectMake(5, 3, tableView.bounds.size.width - 5, 18)] autorelease];
    label2.text = @"Search results";
    label2.textColor = [UIColor whiteColor];
    label2.backgroundColor = [UIColor clearColor];
    [headerView addSubview:label2];
}
return headerView;

}
4

1 に答える 1

2

self.searchResultsだから、それは常に非であるように聞こえnilます。

交換

if (self.searchResults == nil){

if ([self.searchResults count] == 0){

交換してください

else if (self.searchResults != nil){

else {
于 2012-10-21T15:30:06.870 に答える