当滚动表格视图单元格的视图,单元格文本更改
问题描述:
我有一个UITableView和我编程地向单元格添加两个按钮。 1按钮添加到单元格文本(向上计数)另一个减去1(倒计数)。但是,可以说我添加4,单元格的文本将为4
,但是当我将该单元格向上滚动并从视图中移出时,当它回到视图中时,单元格文本将返回到1
,这是它开始的位置。同样的情况发生,如果我添加(它也是相同的,如果我减去)单元格文本和切换页面,然后返回到表视图。这里是cellForRow
:当滚动表格视图单元格的视图,单元格文本更改
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
newBtn = [[UIButton alloc]init];
newBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[newBtn setFrame:CGRectMake(260,20,55,35)];
[newBtn addTarget:self action:@selector(subtractLabelText:) forControlEvents:UIControlEventTouchUpInside];
[newBtn setTitle:@"-" forState:UIControlStateNormal];
[newBtn setEnabled:YES];
[cell addSubview:newBtn];
subBtn = [[UIButton alloc]init];
subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[subBtn setFrame:CGRectMake(200,20,55,35)];
[subBtn addTarget:self action:@selector(addLabelText:) forControlEvents:UIControlEventTouchUpInside];
[subBtn setTitle:@"+" forState:UIControlStateNormal];
[subBtn setEnabled:YES];
[cell addSubview:subBtn];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.imageView.image = [imageArray objectAtIndex:indexPath.row];
cell.textLabel.text = [cells objectAtIndex:indexPath.row];
return cell;
}
任何和所有帮助表示赞赏!谢谢:d
方法按钮
- (IBAction)addLabelText:(id)sender{
cell = (UITableViewCell*)[sender superview];
cell.textLabel.text = [NSString stringWithFormat:@"%d",[cell.textLabel.text intValue] +1];
}
- (IBAction)subtractLabelText:(id)sender
{
cell = (UITableViewCell*)[sender superview];
if ([[cell.textLabel text] intValue] == 0){
cell.textLabel.text = [NSString stringWithFormat:@"%d",[cell.textLabel.text intValue] +0];
}
else{
cell.textLabel.text = [NSString stringWithFormat:@"%d",[cell.textLabel.text intValue] -1];
//[myTableView reloadData];
}
}
答
你需要这样的事情,你在哪里储存在细胞外的值。这是因为细胞被重复使用,并且不能长期保存。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
subBtn = [[UIButton alloc]init];
subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[subBtn setFrame:CGRectMake(200,20,55,35)];
[subBtn addTarget:self action:@selector(addLabelText:indexPath.row) forControlEvents:UIControlEventTouchUpInside];
[subBtn setTitle:@"+" forState:UIControlStateNormal];
[subBtn setEnabled:YES];
[cell addSubview:subBtn];
}
// we're loading the value from the array each time the cell is displayed.
cell.textLabel.text = [cellLabelValues objectAtIndex:indexPath.row];
return cell;
}
- (IBAction)addLabelText:(int)currentRow{
NSString *newValue = [NSString stringWithFormat:@"%d",[[[cellLabelValues objectAtIndex:currentRow] intValue] +1];
// we update the value in the array since this is the source of the data for the cell
[cellLabelValues replaceObjectAtIndex:currentRow withObject:newValue];
// now reload to get the new value
[myTableView reloadData];
}
这是因为细胞被重用,你需要(重新)中的cellForRowAtIndexPath设置的东西(标签,按钮),以确保,当细胞被重用,对细胞的所有组件都是正确的。例如,如果单元格上有一个按钮,并且其文本每行都不相同,则该文本需要在cellForRowAtIndexPath中重置;如果所有行的文本都是相同的,显然没有什么可担心的。 –
非常感谢帮助彼得!你如何建议我重置'cellForRowAtIndexPath'中的单元格文本?非常感谢! :D – iProRage