开发者

Determine the last selected row when returning from detailView of UITableView

Ok I push a detailView when row is selected in the UITableView. In the detailView I set som data and pas开发者_运维百科s that to the webservice. I want to retrieve some data either before the detailView is pushed or when it will disapear. Based on the data retrieved will I set an UIImage as an indicator.

I wonder how I can determine the indexpath.row the detailView got pushed for and use that for setting the UIImage. Is this possible?

I have a UISegmentedControl which loads two different datasources for the UITableView.

For testing I pass the indexpath.row when the detailView is pushed and callback to the UITableView class along with a boolean if the UIImage should be display.

But my problem is that indexpath.row is 0 when the UITableView loads. And when changing datasource back and forth and passing the indexpath.row with it, the indicator will be display on wrong rows.

The two datasources for the table is:

if (index == 0) {

    dictionary = [firstArray objectAtIndex:indexPath.row];
}
else {

    dictionary = [secondArray objectAtIndex:indexPath.row]; 
}

I then set the indicator for that row (will be the UIImage later):

if (indexPath.row == selectedIndex) {
    cell.indicator.text = @"Begin";
}

When the detailView is pushed in didSelectRowAtIndexPath i pass in the indexPath.row:

NSInteger rowIndex = indexPath.row;
[self.detailViewController initWithDetailsSelected:rowIndex:dictionary];

When the detailView is done the UITableView class receives the callback:

 - (void)processAttendanceSuccessful:(BOOL)successful:(NSInteger)_selectedIndex{

self.selectedIndex = _selectedIndex;
 }

So based on a boolean a UIImage should be display for the correct row in the UITableView. Any suggestions how I can go about this. It must be quite common to set some value in a detailedView and display some data for a specific row in the UITableView, but having a rather hard time finding any good tutorials for this. I only find how to pass data into the detailView, which I dont have any problem with.

Thanks in advance people.


Don't deselect the row in tableView:didSelectRowAtIndexPath:. When you return to this view controller, just use indexPathForSelectedRow method of the UITableView object.

But in the long run this makes little sense. Since the table view reuses those cells, you also need to remember the rows that have the image. You will probably have to maintain an array for this. In which case, you can either pass the array and the index path to the detail view controller and update them there or use delegate or notification mechanism to update the array from within the table view controller.


I would use an ivar of int, using negative values to represent null selection:

@interface MyController : UIViewController {

int lastSelection;

}
...
@end

@implementation MyController
...
- (void)initWithBlablabla{
    ...
    lastSelection = -1;
}

...

@end

However, I think you should better re-think about your design referring to KVO programming guide. In general MVC patterns, a controller is not the right place to store the state of the model.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜