0

を持つアプリがありUITableViewます。は、別のクラスでUITableViewカスタムUITableViewCells定義されていCustomCellます。これらのセルは、UITableViewで定義されたボタンによって に追加されますUIViewController。セルには がありUIButton、セルの作成時に選択され、ボタンを押すたびに選択状態が変わります。各セルNSMutableArrayの選択状態を含む を作成する必要があります。UIButtonテーブル ビューの行 n のボタンの選択状態は、のインデックス n になりNSMutableArrayます。ボタンが選択されている場合は配列に1を追加し、選択されていない場合は0を追加します。その配列を使用して のデータを更新し、UITableView後で計算を行います。

私の問題:

-ボタンが別のクラスで定義されているため、ボタンを押したときにボタンに固有の配列の値を変更する方法がわかりません。

-n 番目の行でボタンを選択すると、8 行ごとにそれらの行のボタンが選択されます。これらのセルをまだ作成していなくても、作成するとボタンが選択されます。(たとえば、行 1 のボタンをクリックすると、行 9、17、25 のボタンも選択/選択解除されます)

-配列を使用するために私が見つけた唯一の方法は、必要なたびに新しい配列を作成することでしたUITableView。ボタンが選択/選択解除された場合は、の各セルを繰り返し、1/0 を追加します。もっと良い方法があると確信しています。

ここに私のコードがあります:

CustomCell.h

@interface CustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UIButton *tickButton;

CustomCell.m

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    return self;
}

- (IBAction)tick:(UIButton *)sender {

    if ([sender isSelected]) {
        [sender setImage:[UIImage imageNamed:@"off"] forState:UIControlStateNormal];
        [sender setSelected:NO];
        _isTicked = [NSNumber numberWithInt:0];
    }
    else {
        [sender setImage:[UIImage imageNamed:@"on"] forState:UIControlStateSelected];
        [sender setSelected:YES];
        _isTicked = [NSNumber numberWithInt:1];
    }

}

MainViewController.h

@interface SplitBillViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UIBarButtonItem *addItem;
@property (strong, nonatomic) IBOutlet UITableView *itemTable;

MainViewController.m

@implementation MainViewController{
    BOOL lastButtonPressed;
    NSInteger n;
    NSNumber *a;
}


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

    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) return n;
    return 4;
}

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

        CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
            cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        }


        if ([indexPath row]%2==0){
            [cell setBackgroundColor:[UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:0.8]];
        }
        else{
            [cell setBackgroundColor:[UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:0.9]];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryView = [UIView new];
        return cell;
    }

    }

}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        lastButtonPressed=NO;
        --n;
        [_itemTable reloadData];
}

- (IBAction)addItem:(UIBarButtonItem *)sender {
    lastButtonPressed=YES;
    ++n;
    [_itemTable reloadData];
    [_itemTable scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:n-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    [_itemTable endEditing:YES];
}

-(NSMutableArray*) returnTickArray{
    NSMutableArray *tickArray = [[NSMutableArray alloc] initWithCapacity:n];
    for (NSInteger i=0; i<n; i++){
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
    CustomCell *cell = (CustomCell *) [self.itemTable cellForRowAtIndexPath:indexPath];
       // NSLog([NSString stringWithFormat:@"%@",cell.isTicked]);
        if ([cell.tickButton isSelected]){
            a=[NSNumber numberWithInt:1];
        }
        else {
            a=[NSNumber numberWithInt:0];
        };
        [tickArray addObject:a];
    }
    return tickArray;
}

@end

もちろん、もっと多くのコードがありますが、何にも影響しないと確信しているものは何もコピーしませんでした。

ありがとう!

4

3 に答える 3

1

このように試してみると、

1.選択したボタン用に1つNSMutableArray取ります。

2.その時点で特定のボタンを選択すると、そのindexpath.row値が配列に追加されます。

3.その時点でボタンを選択解除すると、その配列に存在する indexpath.row 値をチェックし、存在する場合は、そのオブジェクトのインデックスを見つけて、そのオブジェクトを配列から削除します。

if([array containsObject:@"value"]){
        int n=[array indexOfObject:@"value"];
        [array removeObjectAtIndex:n];

    }

ボタンをクリックすると、このメソッドが呼び出されます。このメソッドでは、すべてのシンを管理できます。

- (IBAction)tick:(UIButton *)sender {

//here you will get tag value like sender.tag value.

if ([sender isSelected]) {
        [sender setImage:[UIImage imageNamed:@"off"] forState:UIControlStateNormal];
        [sender setSelected:NO];
        _isTicked = [NSNumber numberWithInt:0];

       NSString *value=[NSString stringWithFormat:@"%d",sender.tag];
       if([array containsObject:value]){
          int n=[array indexOfObject:value];
          [array removeObjectAtIndex:n];

       }
    }
    else {
        [sender setImage:[UIImage imageNamed:@"on"] forState:UIControlStateSelected];
        [sender setSelected:YES];
        _isTicked = [NSNumber numberWithInt:1];
        [array addObject:[NSString stringWithFormat:@"%d",sender.tag]];//if already present in array the skip this one
    }
}

それ以外の場合は、このように使用して indexPath 値を直接取得します。

NSIndexPath *indexPath = [yourtableviewname indexPathForCell:(UITableViewCell *)sender.superview];
    NSLog(@"%d",indexPath.row);
于 2013-05-07T04:23:37.257 に答える
0

-n 番目の行でボタンを選択すると、8 行ごとにそれらの行のボタンが選択されます。これらのセルをまだ作成していなくても、作成するとボタンが選択されます。(たとえば、行 1 のボタンをクリックすると、行 9、17、25 のボタンも選択/選択解除されます)

それは細胞が再利用されるからです。のレベルでこれについて心配する必要がありますtableView:cellForRowAtIndexPath:。ここのセルごとに、つまりこのメソッドが呼び出されるたびに、ボタンを選択するかどうかを指定する必要があります。

于 2013-05-07T04:20:12.340 に答える