2

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;
}

これは私が望むようにほとんど機能しています. theArrayandの内容thePriceArrayが表示されますが、長いリストでは次のようになります:

ここに画像の説明を入力

「キーと値」を 1 つのセルに入れたいので、価格と商品名を 1 つのセル (Hat - 30) に入れ、別々にしないようにするにはどうすればよいですか? を使用しようとしましたNSDictionaryが、運が悪かったので、NSDictionaryこれに使用できますか?

4

3 に答える 3

1

favItemとfavPrizeの2つの新しい配列を作成できます。お気に入りのアイテムをfavItem配列に追加し、お気に入りの賞品をfavPrize配列に追加するだけです。次に、これらの配列を使用して、テーブルビューのラベルとdetailLabelsを次のように設定します。

cell.textLabel.text = [favItems objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [favPrize objectAtIndex:indexPath.row];
于 2012-12-31T11:50:40.533 に答える
1

theArray配列との両方を保持thePriceArray seperateし、一方をtableViewのデータソースのメイン配列として使用します。

:NSUserDefaultからそれぞれのアレイにデータを追加します。

これでメソッドは次のようになります:

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [theArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"TheTableCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubTile reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.text = [theArray objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [thePriceArray objectAtIndex:indexPath.row];
    return cell;
}
于 2012-12-31T11:55:54.833 に答える
1

次のコードを使用して簡単に行うことができます。

NSString *label = [NSString stringWithFormat:@"%@    %@",[theOrderList objectAtIndex:(indexPath.row * 2)],[theOrderList objectAtIndex:(indexPath.row * 2)+1]
cell.textLabel.text = label;

そしてまたあなたのnumberOfRowsInSectionように変更します:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [theOrderList count]/2;
}
于 2012-12-31T11:56:52.037 に答える