を持つアプリがあり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
もちろん、もっと多くのコードがありますが、何にも影響しないと確信しているものは何もコピーしませんでした。
ありがとう!