0

iPhoneアプリで次のようなビューを作成したい:

ここに画像の説明を入力してください

iOSでこのコントロールが何であるか正確にはわかりません。おそらく、左側にアイコンとテキストを設定し、右側に小さな記号を設定できます。

TableViewを実装しましたが、次のように設定できました。

    [[cell textLabel] setText:customer.name];
    [[cell textLabel] setTextColor:[UIColor blackColor]];
    [[cell imageView] setImage:[UIImage imageNamed:@"icon.png"]];
    //[[cell detailTextLabel] setText:@"Awsome weather idag"];
    cell.accessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];  

しかし、どうすればそれを写真のそのビューのように機能させることができますか?

4

3 に答える 3

2

非常に簡単です。以下の手順に従ってください。疑問がある場合は、UITableView ドキュメントを確認してください。

1.グループ化されたテーブルビューを作成します。

プログラムで:

CGRect tableFrame = CGRectMake(0, 0, 200, 200);

UITableView *tableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewGroupedStyle];
tableView.delegate = self;
tableView.dataSource = self;

[self.view addSubview:tableView];

UITableViewControllerサブクラスの割り当て(一般的なケース):

MyTableViewController *controller [[MyTableViewController alloc] initWithStyle: UITableViewGroupedStyle];

[self.navigationController pushViewController:controller animated:NO];

インターフェイスビルドを介して:

  1. ユーティリティメニュー(右上隅のアイコン)を展開します。
  2. テーブルビューを選択します(クリックします)。
  3. 属性インスペクター(右上隅の4番目のアイコン)をクリックします。
  4. [テーブルビュー]で、[スタイル]ドロップダウンをクリックし、[グループ化]を選択します。

2.UITableViewDataSourceプロトコルを実装します。

基本的に、この3つの関数をコントローラーに追加します。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in a given section.

    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // Configure the cells.

    return cell;
}

3.セルの構成:

UITableViewCellのデフォルトのスタイルには、画像ビュー(UIImageView)、タイトルラベル(UILabel)、およびアクセサリビュー(UIView)があります。提供した画像にテーブルビューを複製するために必要なものはすべてあります。

だから、あなたはこのようなものを探していますtableView:cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString * const cellIdentifierDefault = @"default";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierAccount];

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

    if (indexPath.section == 0) {

        cell.imageView.image = [UIImage imageName:@"bluetooth_icon"];
        cell.textLabel.text = @"Bluetooth";

        // Additional setup explained later.

    }else{

        if (indexPath.row == 0) {

            cell.imageView.image = [UIImage imageName:@"general_icon"];
            cell.textLabel.text = @"General";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        }else{

            cell.imageView.image = [UIImage imageName:@"privacy_icon"];
            cell.textLabel.text = @"Privacy";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        }

    }

    return cell;
}

プロパティaccessoryTypeは、セルの右側に表示される内容を定義します。アクセサリタイプのリストは、ここにあります。

最初のセル(Bluetooth)で、カスタムアクセサリビューを作成し、それをセルのaccessoryViewプロパティに割り当てる必要があります。これを実現する方法の非常に素朴な例を以下に示します。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString * const cellIdentifierDefault = @"default";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierAccount];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierAccount];

        label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
        label.textAlignment = NSTextAlignmentRight;

        cell.accessoryView = label;

    }else{

        label = (UILabel *) cell.accessoryView;

    }

    cell.imageView.image = [UIImage imageName:@"bluetooth_icon"];
    cell.textLabel.text = @"Bluetooth";
    label.text = @"Off";

    return cell;
}

これがお役に立てば幸いです、マテウス

于 2012-10-08T01:35:28.637 に答える
2

高速出力には、 https://github.com/escoz/QuickDialogなどのライブラリを使用でき ます

私の経験では、このようなソリューションでは、変更が加えられたときに、より複雑になります。

特定のビューで特定のラベルを1つだけ変更したい場合もありますが、既成のソリューションでは簡単ではありません。

于 2012-10-08T05:47:49.203 に答える
1

UITableViewセクションを調べます。それがグループを分離するものです。

于 2012-10-07T23:08:26.607 に答える