我有一个UITableView
的应用。 UITableView
在另一个类UITableViewCells
中定义了自定义CustomCell
。这些单元格通过UITableView
中定义的按钮添加到UIViewController
。在单元格中有一个UIButton
,它们在创建单元格时被选中,并且每次按下按钮时它们都会改变选择状态。我需要创建一个NSMutableArray
,其中包含每个单元格的UIButton
的选定状态。表视图的行n中按钮的选定状态将位于NSMutableArray
的索引n中。如果按钮被选中,我想向数组添加1,如果没有,则添加0。我将使用该数组刷新UITableView
中的数据并稍后进行一些计算。
我的问题:
- 我不知道如何更改特定于我按下的按钮的数组值,因为按钮是在另一个类中定义的。
- 如果我在第n行选择一个按钮,那么每隔8行就会选中这些行上的按钮,即使我还没有创建这些单元格,当我创建它们时,按钮将被选中。 (例如,如果我点击第1行上的按钮,第9,17,25行上的按钮也会被选中/取消选择)
- 我发现使用数组的方法是每次需要时创建一个新的,通过迭代UITableView
的每个单元格并添加1s / 0s(如果选择/取消选择按钮)。我确信有更好的方法。
继承我的代码:
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
当然,还有更多代码,但我没有复制任何我认为不会影响任何内容的内容。
谢谢!
答案 0 :(得分:1)
一旦尝试这样,
1.为选择按钮选择一个NSMutableArray
。
2.当您在此时选择特定按钮时,将indexpath.row
值添加到数组中。
3.当您在此时取消选择按钮时,检查该数组中是否存在indexpath.row值,如果是,则查找该对象的索引并从该数组中删除该对象。
if([array containsObject:@"value"]){
int n=[array indexOfObject:@"value"];
[array removeObjectAtIndex:n];
}
当yiu点击按钮时,此方法将调用,在此方法中,您可以管理所有细节,
- (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);
答案 1 :(得分:0)
- 如果我在第n行选择一个按钮,那么每隔8行就会选中这些行上的按钮,即使我还没有创建这些单元格,当我创建它们时按钮&#39 ;将被选中。 (例如,如果我点击第1行上的按钮,第9,17,25行上的按钮也会被选中/取消选择)
这是因为细胞被重复使用。您需要在tableView:cellForRowAtIndexPath:
级别担心这一点。对于此处的每个单元格,意味着每个时间调用此方法,您需要指定是否选择该按钮。
答案 2 :(得分:0)
您可以执行以下步骤 您需要一个数组来跟踪所选按钮。我将保存该数组中相应按钮的indexPath。创建一个数组作为属性
@property (nonatomic,retain) NSMutableArray* selectedIndexPaths;
// In viewDidLoad
self.selectedIndexPaths = [[NSMutableArray alloc]init];
在 cellForRowAtIndexPath 中 以编程方式添加按钮。无需在自定义单元格类中添加它
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
cell.textLabel.text= [NSString stringWithFormat:@"%d", indexPath.row];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Press Me" forState:UIControlStateNormal];
button.frame = CGRectMake(200, 10, 100,30);
button.tag = indexPath.row;
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
[cell bringSubviewToFront:button];
}
按下按钮
在selectedIndexPath数组中添加/删除按钮的indexPath
-(void)buttonPressed:(UIButton *)sender
{
NSLog(@"Button Pressed");
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell*)sender.superview];
if([self.selectedIndexPaths containsObject:indexPath])
{
[self.selectedIndexPaths removeObject:indexPath];
}
else
{
[self.selectedIndexPaths addObject:indexPath];
}
NSLog(@"SelectedIndexPaths : %d",self.selectedIndexPaths.count);
}
现在您拥有所有选定按钮单元格的indexPath。做你想做的事