Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Albright

macrumors regular
Original poster
Aug 23, 2011
130
299
As the title implies, I'm very much a newbie, and this is the first iOS project I've worked on without exactly following the instructions of my Hillegass book. Unfortunately, I'm stumped on something pretty early on. The gist of the app I'm trying to build is that you can enter a search term, and the app will query the English Wikipedia's API for the titles of that same article on other languages' versions of Wikipedia - so it's basically abusing Wikipedia to use it as a multi-lingual dictionary. I've implemented this as a full-screen UITableView which has a single section with a text field in its header where you enter the search term. The results are fetched and put into the table as table cells.

The problem is that when you enter a term which causes the table to fill with more rows than can be shown on screen ("apple" is one I use often for testing since it has about a zillion results (the fruit, not the company)), the table does not scroll correctly; it doesn't move on a one-on-one ratio with my finger movement, and the scroll bar doesn't appear. And when I let go, it scrolls back to the top position - so basically it's reacting as if the table didn't have enough rows to fill the screen.

I think the problem might be that I'm not allocating the window size to the correct bounds, or perhaps that I need to reallocate its bounds after the table is populated, but in either case I'm not sure what the correct behavior should be.

I've attached my project files, if you can be so kind to take a look. Also, if you have a Stack Overflow account and want some points, you can also answer my same question over there (I posted it a few days ago and haven't got an answer yet). Thanks in advance.
 

Attachments

  • WikipediaTrans.zip
    40.3 KB · Views: 76

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
I haven't looked at your project.

Size of window and bounds of table view are not related to this.

I assume that you are dynamically adding rows to the table. How are you doing this? All at once? One at a time? Do you insert new rows or call reloadData? What is your data model?
 

Albright

macrumors regular
Original poster
Aug 23, 2011
130
299
I assume that you are dynamically adding rows to the table. How are you doing this? All at once? One at a time? Do you insert new rows or call reloadData? What is your data model?

I have a data store class (I think that's the right terminology) that basically holds an array of results. After fetching JSON-formatted data from the tubes, my code is parsing it and populating that array with the data, then calling reloadData on the table after that. My cellForRowAtIndexPath implementation gets that array from the data store and formats a cell for the respective result (using the dequeue stuff, which I must confess I don't entirely understand yet). All of the rows appear in the table as expected, so that's working fine; it's just the scrolling behavior of the table after the rows are added that's puzzling me.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Use a navigation controller as your rootViewController. You can hide the navigation bar if you like. I think that will fix things.

Alternatively, in didFinishLaunchingWithOptions: don't create the window as the same size as the tableview bounds. Create the window as the same size as the mainScreen bounds. You might want to make a new project and look at how the window is created.

Anyway. I did both those things to your code. The navigation controller fixed the problem, although I'm not exactly sure if there's a way to make the TableViewController act correctly without the nav controller.


Code:
{
  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  // Override point for customization after application launch.
  
  WordsViewController *viewController = [[WordsViewController alloc] init];
  UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:viewController];
  [[self window] setRootViewController:navController];

  self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
...although I'm not exactly sure if there's a way to make the TableViewController act correctly without the nav controller.

I tried setting the rootViewController to a WordsViewController, added a call to startSearch:mad:"apple" in the viewDidLoad (for now), and commented out the viewForHeaderInSection: method and the scrolling worked as expected. If you uncomment it, the scrolling is incorrect again. It definitely seems to be some strange interaction between the tableView and the headerView that is messing up the scrolling.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Albright, there is definitely something weird with your WordsHeaderView.xib. I replaced NIB-loading it with some quick-and-dirty code to (mostly) recreate it (as well as removing the __weak directive on the ivar declaration) and I got the scrolling to work as expected. I can't (so far) nail down why that XIB is funky though. Here's my replaced code:
Code:
- (UIView *)headerView {
  if (!wordsHeaderView) {
//    [[NSBundle mainBundle] loadNibNamed:@"WordsHeaderView" owner:self options:nil];
	  wordsHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
	  wordsHeaderView.backgroundColor = [UIColor orangeColor];
	  searchField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
	  searchField.borderStyle = UITextBorderStyleRoundedRect;
	  [searchField addTarget:self action:@selector(searchFieldSearch:) forControlEvents:UIControlEventEditingDidEnd];
	  searchField.delegate = self;
	  [wordsHeaderView addSubview:searchField];
  }
  return wordsHeaderView;
}

EDIT:
I would try recreating your WordsHeaderView.xib piece-by-piece with testing after each change and seeing if you can isolate the one change that causes things to go awry.

P.S. Why did you add the __weak to the searchField ivar?
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.