0

まず、私は iOS 開発が初めてで、これにアプローチする方法を理解しようとしています。

このスクリーングラブは別のアプリからのものです。緑色のプラス ボタンを押すと、緑色のプラス ボタンを押した回数に応じて、上部にあるようなボタンが追加されます

ここに画像の説明を入力

これを複製しようとしていますが、どこから始めればよいかわかりません。私の質問は、一番上のボタンのサブクラスを作成するのが最善でしょうか? そして、緑色のプラスボタンが押されるたびに、そのサブクラスボタンを何らかの方法で追加しますか?

4

1 に答える 1

1

私はこれを自分で学びたかったので、サンプルプロジェクトを作成しました。これを行う方法の簡単なチュートリアルを次に示します。完璧ではありませんが、これがどのように機能するかの基本的なアイデアが得られるかもしれません。


Interface Builder で Table View Controller を作成します。次に、ボタンとテキスト フィールドをプロトタイプ セルに追加します。

テーブル ビュー コントローラーとプロトタイプ セル


プロジェクトに新しいクラスを追加します。MyTableViewController という名前で呼び出しましょう。テーブル ビュー コントローラーのクラスを MyTableViewController に設定します (インターフェイス ビルダーで)。

ここに画像の説明を入力


プロトタイプ セルの識別子を設定します。値を使用defaultCellしました。

ここに画像の説明を入力


ボタンとテキスト フィールドのタグ値を設定します。

ここに画像の説明を入力

これらのタグ値を使用しました:

  • プラスボタン:100
  • タイムボタン:110
  • アイテムボタン:120
  • テキストフィールド 1: 10
  • テキストフィールド 2: 20
  • テキストフィールド 3: 30

MyTableViewController.h (注: UITableView はtView変数としてリンクされています)

#import <UIKit/UIKit.h>

@interface MyTableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>

/* Data array */
@property (nonatomic) NSMutableArray *data;
/* Expanded cell */
@property (nonatomic) NSInteger expandedCell;

/* Table view */
@property (strong, nonatomic) IBOutlet UITableView *tView;

@end 

MyTableViewController.m

#import "MyTableViewController.h"

@interface MyTableViewController ()

@end

@implementation MyTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    /* No cells expanded by default */
    _expandedCell = -1;

    /* Create data array */
    _data = [NSMutableArray new];

    /* Add two cells to data array */
    UITableViewCell *cell = [self newDefaultCell:0];
    [_data addObject:cell];
    UITableViewCell *cell2 = [self newDefaultCell:1];
    [_data addObject:cell2];
}

- (UITableViewCell *)newDefaultCell:(NSUInteger)index {

    /* Initialize new UITableViewCell using prototype cell */
    UITableViewCell *cell = [_tView dequeueReusableCellWithIdentifier:@"defaultCell"];

    /* Initialize buttons for this cell */
    UIButton *plusButton = (UIButton *)[cell viewWithTag:100];
    [plusButton setTag:index];
    [plusButton addTarget:self action:@selector(plusButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    UIButton *timeButton = (UIButton *)[cell viewWithTag:110];
    [timeButton setTag:index];
    [timeButton addTarget:self action:@selector(timeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    UIButton *itemButton = (UIButton *)[cell viewWithTag:120];
    [itemButton setTag:index];
    [itemButton addTarget:self action:@selector(itemButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [_data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /* Returns UITableViewCell object from data array */
    return [_data objectAtIndex:indexPath.row];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    /* If cell is expanded, return row height 140 */
    if(_expandedCell == indexPath.row) {
        return 140;
    } /* else return row height 30 */
    else return 30;
}

- (void)plusButtonPressed:(id)sender {
    /* Create new UITableViewCell */
    UITableViewCell *newCell = [self newDefaultCell:[_data count]];
    /* Add UITableViewCell to data array */
    [_data addObject:newCell];

    /* Update table view */
    [_tView beginUpdates];
    [_tView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:([_data count]-1)inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
    [_tView endUpdates];
}

- (void)timeButtonPressed:(id)sender {
    UIButton *button = (UIButton*)sender;

    /* Expand this cell to show UITextFields */
    _expandedCell = [button tag];

    /* UITableViewCell from data array, index is button's tag value */
    UITableViewCell *cell = [_data objectAtIndex:[button tag]];

    /* You can access text fields using viewWithTag: call */
    UITextField *tf1 = (UITextField *)[cell viewWithTag:10];
    UITextField *tf2 = (UITextField *)[cell viewWithTag:20];
    UITextField *tf3 = (UITextField *)[cell viewWithTag:30];

    /* Reload UITableViewRow */
    NSArray *indexes = @[[NSIndexPath indexPathForRow:[button tag] inSection:0]];
    [_tView reloadRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationTop];
    [_tView beginUpdates];
    [_tView endUpdates];
}

- (void)itemButtonPressed:(id)sender {

}

@end

そして、それは完了です。

于 2013-11-14T02:13:14.053 に答える