开发者

How to load the right nibs when selecting a sorted row?

I sort the UITableView as in this answer How to sort alphabetically a UITableView Sectioned?.

The problem is now that UITableView doesn't push out the correct ViewControllers, due i have 2 localizations, English and Italian in my app. For english it works fine, but for italian not.

The code 开发者_如何学CI'm using is this,as suggested in this answer Pushing View Controllers in UITableViewController Grouped:

if (indexPath.section == 0) 
{   
     switch (indexPath.row) {
        case 0:
        [self.navigationController pushViewController:[[[FirstViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES];
        break;

        case 1:
        [self.navigationController pushViewController:[[[SecondViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES];
        break;
        // and so on
    }
} 

Any ideas for this?


Your problem is totally logical and does not need a change in your code. When you sort the tableView array, it is trivial that didSelectRowAtIndexPath would get messed up while selecting rows.

This might be because you would have implemented something like

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

if (indexPath.section == 0)
   {
    if (indexPath.row ==0)
         // PushViewController to view 1 //

    if (indexPath.row ==1)
         // PushViewController to view 2 //

   }

if (indexPath.section ==1)
   // Do something like before

If this is your case, then yes, when your sort the tableView you will push view controllers in a BAD WAY.

My suggestion is that you can take some properties in each row and check for that while pushing view controllers. For example, if you are... (say) having row 1 to be a certain letter or a number or a word, you can check for that and pushViewController accordingly.

But this will work only if you know all the elements in the tableView (never mind sorting).

You can check something like...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

 UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

 if (selectedCell.textLabel.text = @"1")
   // push view controller to view 1

 if (selectedCell.textLabel.text isEqualToString: self.lifeStyle) 
    // push view controller to lifeStyleViewController

    // here lifeStyle is a NSString property // 

You can check for anything if you logically set and assign your properties in the cellForRowAtIndexPath method and logically set it such that when you perform a tableView reloadData after sorting, the flow of the didSelect would still be the same.

Let us know if you still have problems.


I can be wrong, but I'm pretty sure that yours sortings, regarding English and Italian, will place the cells in a different order. So you have to sort another array accordingly, with the nibs reference. I guess your first row in english is not the same one as in italian, or am I wrong ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜