tableview
を使用して、次のコードを使用して、アプリにブックマーク/お気に入り機能、 からのお気に入り を実装しましたNSUserDefaults
。セルには、アイテムの名前と価格の 2 つのラベルがあります - 配列に格納されています -theArray
そしてthePriceArray
-> 次のように:
NSIndexPath *indexPath = [_ListTableView indexPathForCell:(UITableViewCell *) [[sender superview] superview]];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *OrderList = [[defaults objectForKey:ORDER_KEY] mutableCopy];
if (!OrderList) OrderList = [NSMutableArray array];
[OrderList addObject:[theArray objectAtIndex:indexPath.row]];
[OrderList addObject:[thePriceArray objectAtIndex:indexPath.row]];
[defaults setObject:OrderList forKey:ORDER_KEY];
[defaults synchronize];
私は今、2 つの配列 theArray
&thePriceArray
を に追加していますNSMutableArray
。これらの情報 (配列からの情報) を別の に表示したいと思いtableview
ます。私はこれを次のようにします:
私の中でviewDidAppear
:
NSUserDefaults *defaults = kSettings;
NSMutableArray *theOrderList = [NSMutableArray arrayWithArray:[defaults objectForKey:ORDER_KEY]];
そして、の内容を表示するにはtableview
:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [theOrderList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"TheTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [theOrderList objectAtIndex:indexPath.row];
return cell;
}
これは私が望むようにほとんど機能しています. theArray
andの内容thePriceArray
が表示されますが、長いリストでは次のようになります:
「キーと値」を 1 つのセルに入れたいので、価格と商品名を 1 つのセル (Hat - 30) に入れ、別々にしないようにするにはどうすればよいですか? を使用しようとしましたNSDictionary
が、運が悪かったので、NSDictionary
これに使用できますか?