滚动 UITableList 时更改复选框位置 [英] Changing Check boxes positions when scrolling UITableList
本文介绍了滚动 UITableList 时更改复选框位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的 tableList 上插入了一个 Button 来检查和取消选中产品,为此我使用了下面的代码,但是当我滚动我的 tableList 时,已经选中的框被取消选中并且它们的位置也在改变
I have inserted one Button on my tableList for check and unchecked the products, For this I have used below code but when I scroll my tableList already checked boxes are un-checking and their positions also changing
我该如何解决这个问题?
How can I solve this problem?
我的代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return mainArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"HistoryCell";
UITableViewCell *cell = (UITableViewCell *)[MaintableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
model1 = mainArray[indexPath.row];
newBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[newBtn setFrame:CGRectMake(250,5,30,30)];
[newBtn addTarget:self action:@selector(urSelctor:) forControlEvents:UIControlEventTouchUpInside];
UIImage *btnImage;
if(model1.ischeck){
btnImage = [UIImage imageNamed:@"check.png"];
}else{
btnImage = [UIImage imageNamed:@"uncheck.png"];
}
[newBtn setImage:btnImage forState:UIControlStateNormal];
[cell addSubview:newBtn];
return cell;
}
-(void)urSelctor :(UIButton*)sender{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:MaintableView];
NSIndexPath *indexPath1 = [MaintableView indexPathForRowAtPoint:buttonPosition];
NSInteger variable = indexPath1.row;
model1 = mainArray[variable];
if (model1.ischeck){
model1.ischeck = NO;
[mainArray replaceObjectAtIndex:variable withObject:model1];
forState:UIControlStateNormal];
}
else{
model1.ischeck = YES;
[mainArray replaceObjectAtIndex:variable withObject:model1];
forState:UIControlStateNormal];
}
[MaintableView reloadData];
}
@end
模型对象:-
@interface ModelObject1 : NSObject{
BOOL ischeck;
}
@property(nonatomic) BOOL ischeck;
@end
推荐答案
您可以使用此代码:
-(void)btnPressed :(id)sender
{
CGPoint point=[sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *index= [self.tableView indexPathForRowAtPoint:point];
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:index];
[category manageCell:index.row];
[self.tableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationAutomatic];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *const cellIdentifier=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier ];
}
UIButton *checkBtn=[cell.contentView viewWithTag:100];
[checkBtn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
if([category.indexArray containsObject:[NSNumber numberWithInteger:indexPath.row]])
{
[checkBtn setImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
}
else
{
[checkBtn setImage:[UIImage imageNamed:@"uncheck"] forState:UIControlStateNormal];
}
return cell;
}
-(void)manageCell:(NSInteger)index
{
if(![_indexArray containsObject:[NSNumber numberWithInteger:index]])
{
[_indexArray addObject:[NSNumber numberWithInteger:index]];
}
else
{
[_indexArray removeObject:[NSNumber numberWithInteger:index]];
}
NSLog(@"%@",_indexArray);
}
这篇关于滚动 UITableList 时更改复选框位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文