开发者

Select multiple items from a tableview - Beginner

I need to add a TableView and i should be able to click several Items in that tableview and save it to a NSMutable dictionary or something suitable.

I know that you ha开发者_运维知识库ve to use a NSMutable Dictionary for this. But i don't understand how to do this.

Can someone please point a good tutorial or provide some sample codes for me.


You will have to use a delegate method for that.
First make sure your table view is set up well (delegate and datasource) and then
implement delegate 's :
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath.

You access the selected row index with : [indexPath row].
And then you can store this value.


THere was a post by Matt Gallagher at Cocoa With Love about this that you might find illuminating.

A bit dated, but the principles will be the same.


It really depends on your data model. First of all you need a NSMutableArray not a NSMutableDictionary;

//declare an NSMutableArray in the interface
@interface Class : SuperClass {
NSMutableArray *_arrayWithMySelectedItems;
}

//in one of your init/preparation methods alloc and initialize your mutable array;

- (void)viewDidLoad {
[super viewDidLoad];

_arrayWithMySelectedItems = [NSMutableArray alloc] init];
}

//now before you forget it add release in your dealloc method

- (void)dealloc {
[_arrayWithMySelectedItems release];

[super dealloc];
}

//add this following code to your didSelect Method part of tableView's delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//if you have only one category, you will probably have something like this
[_arrayWithMySelectItems addObject:[dataModelArray objectAtIndex:indexPath.row];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}


You can use NSMutableDictionary in this method as:

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *dict = [NSMutableDictionary alloc] init];
    [dict setObject:[NSString stringWithFormat:"%@", cell.Label.text] forKey:[NSString stringWithFormat:"Key%d", indexPath.row]];
}


declare a string inside didselect row of tableview then give that string = [your populated array objectAtIndex:indexpath.row]; then add that into dictionary or nsmutable array according to your wish.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜