0

I am trying to return the number of objects from myArray and depending on the value, change the headerView in my tableView. The app is functioning in the following way, the user searches for an object, if the object exists the object will appear in the tableView. If the user searches for an object that doesn't exist, nothing will appear in the tableView. Pretty easy..

Eventhough the user searches for an object that doesn't exist it seems like the array stores a value in some-way and the tableView/array is not nil anymore? But nothing is displayed in the tableView. What am I doing wrong? searchResults is the array in my 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;

}

1 Answer 1

2

So, it sounds like self.searchResults is always non nil.

Replace

if (self.searchResults == nil){

with

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

and replace

else if (self.searchResults != nil){

with

else {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.