这是我的CellForRowAtIndexPath
代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *simpleTableIdentifier = @"PickAClassCell";
cell = (PickAClassCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PickAClassCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.CountLabel.tag=1000;
[cell.PlusCount addTarget:self action:@selector(PlusButtonSelect :)forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(void)PlusButtonSelect :(UIButton *)sender
{
UILabel *label1 = (UILabel *)[sender.superview viewWithTag:1000];
NSLog(@"%@",label1);
int count = [label1.text intValue];
count = count + 1;
NSString *Strcount= [NSString stringWithFormat:@"%d",count];
label1.text=Strcount;
}
当我点击加号按钮计数值增加但我的问题是当我滚动tableview时,添加的计数值变为零(默认位置)。
答案 0 :(得分:0)
这是因为你写了
int count = [label1.text intValue];
count = count + 1;
每次滚动下一个单元格时,都会使用按钮的初始值初始化计数,而不是使用上一个单元格的按钮int值。
答案 1 :(得分:0)
if ($window.scrollTop() > navShiftBreakpoint) {
$navContainer.addClass( 'full' );}
//full = the second nav & //navShiftBreakpoint = 500px
每次表格视图单元格出现在屏幕上时,此委托调用。尝试在任何变量的帮助下设置计数标签的标记,或尝试通过pickAClassCell添加它。
在你的情况下
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
每次滚动表格视图单元格时都会执行此行。
答案 2 :(得分:0)
试试这个 -
-(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//userCountHolder is instance of NSMutableArray.
userCountHolder = [NSMutableArray arrayWithObjects:@"0",@"0",@"0", nil];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"PickAClassCell";
cell = (PickAClassCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PickAClassCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.CountLabel.text = [userCountHolder objectAtIndex:indexPath.row];
[cell.PlusCount addTarget:self action:@selector(PlusButtonSelect :)forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(void)PlusButtonSelect :(UIButton *)sender{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
int count = [[userCountHolder objectAtIndex:indexPath.row] intValue];
count++;
[userCountHolder replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithInt:count]];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
答案 3 :(得分:0)
在cellForRowAtIndexPath
if ([self.selectedRows containsObject:indexPath])
{
// set count values here
cell.countLabel.text = @"";
cell.textLabel.textColor = [UIColor grayColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
//set the normal cells values
}
此处self.selectedRows
是一个数组,当用户点击加号或减号按钮时,您必须存储cellIndexPath值。