我有一个带有 UITableView
的应用程序。 UITableView
的自定义 UITableViewCells
是在另一个类 CustomCell
中定义的。这些单元格通过 UIViewController
中定义的按钮添加到 UITableView
中。在单元格中有一个UIButton
,它在创建单元格时被选中,并且每次按下按钮时它们都会更改选定状态。我需要创建一个 NSMutableArray ,其中包含每个单元格的 UIButton 的选定状态。 TableView 第 n 行中按钮的选定状态将位于 NSMutableArray 的索引 n 中。如果选择了按钮,我想将 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
当然,还有更多代码,但我没有复制任何我确信不会影响任何内容的内容。
谢谢!
最佳答案
一旦尝试这样,
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);
关于iOS:使用另一个类中定义的 UITableViewCell 中按钮的选定状态填充数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16411044/