views:

93

answers:

4

I have a UITableView which sometimes has only a few rows. I would like a specific row, e.g. the 2nd out of 3, to appear at the top of the table. However, when the number of rows do not fill the entire table, calling the following has no effect:

[TableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexOfTopItem inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO]

Is there a way I can force a row to appear at the top even with a limited number of rows in the table? Thanks

A: 

Rearrange the order, or have a button that shows the previous dates. You can use the numberOfRowsInSection method to check a BOOL, and then set the right number of rows. Then in the cellForRowAtIndexPath method, you again check the BOOL, and return the valid cells. On the button press, set the BOOL, and call the tableView's reloadData method.

jrtc27
For a number of reasons, questions cannot always set out the exact circumstances which require a particular problem to be solved. Making comments about UI or providing answers to questions not asked is rarely helpful. Had I asked in my question if this would have been acceptable or what alternatives I should consider, your answer would have been relevant and useful. If you can't answer the direct question, don't answer. It's inefficient.
Run Loop
Answer edited...
jrtc27
A: 

I'm not sure I understand exactly what you want but if you want to place a particular cell at the top of the table why not rearrange the data instead of the views?

Assuming your cell (row) data is not hard coded you can maybe do something like:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([indexPath row] == 0){
        // Load the data you want to appear at the top
    }else {
        // Load the rest of the data
    }
}

You have to manually keep track of the top row data however so this might not be the best solution.

Alternatively if your data is in an array, you can swap the element you want on top with the first element in the array then call [myTableView reloadData] to reload the data. In this case you don't have to change cellForRowAtIndexPath.

Of course this is not very efficient since you're manipulating arrays and reloading your table but if your table only has a few rows and you don't have to do this swapping often it might be acceptable.

Nebs
My solution lets you place a row on top of a table (as per what the OP wrote 'to appear at the top of the table'). I guess what's really needed is a way for a row to appear at the top of a screen? If that's not the reason for the down vote then please explain.
Nebs
Why would I want to rearrange dates, something which should inherently be sorted ascending or descending? Please see my comment to jrtc27. It's really best to answers only those questions which you can answer directly. My question did not ask for alternative solutions. Reading your answer makes it clear you did not read the comments to the original question.
Run Loop
I did read the comments and the question. I answered your question literally (notice how you ask about placing a row on top of a *table* instead of a *screen*). Of course now that you mention you're storing dates my solution doesn't make much sense, but there was no way to know that originally. So technically this isn't an 'alternative solution' based on the wording of your question and comments. Either way alternative solutions might help some other people so no need to have a negative tone towards people taking the time to help you out.
Nebs
A: 

The only way to achieve this is to set the table's contentOffset. This will ensure the desired row appears at the top of the table, even when the rows do not fill up the entire table.

Run Loop
A: 

When reloadData finished, scrollToRowAtIndexPath would work. 100%, but another thing is to know right moment for this action (scrolling I mean).

Kir