【发布时间】:2012-03-26 23:10:35
【问题描述】:
这个问题我找了很久,没有一个明确的答案。
我的表格视图单元格中有一个UISwitch 作为accessoryView。问题是每次我滚动表格时,开关都会重置回原来的状态。
这是我的cellForRowAtIndexPath 方法:
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
switch1 = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
[switch1 addTarget:self action:@selector(buttonPressed :) forControlEvents:UIControlEventValueChanged];
[switch1 setOn:YES animated:NO];
cell.accessoryView = switch1;
NSString *iconsImage = [[self iconsImages] objectAtIndex:[indexPath row]];
UIImage *cellIcon = [UIImage imageNamed:iconsImage];
[[cell imageView] setImage:cellIcon];
CGRect labelFrame = CGRectMake(65, 18, 150, 25);
UILabel *iconsNameLabel = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
iconsNameLabel.font = [UIFont boldSystemFontOfSize:18];
iconsNameLabel.text = [iconsList objectAtIndex:indexPath.row];
[cell.contentView addSubview:iconsNameLabel];
return cell;
}
顺便说一句,我已经在头文件中声明了我的开关,并将其设置为属性并合成它。
【问题讨论】:
-
你的意思是每次开关滚动*离开屏幕然后重新打开,它又重新打开,还是即使你滚动一点它也会改变?
-
当开关滚出屏幕时它会改变...
-
这就是我根据代码想到的。因为每次出现在屏幕上都是一个新的开关。
-
你有什么建议?...我试过把它放在 if(cell == nil) 但它也不起作用..我不太习惯目标 c 或 iOS...但是我尝试了很多不同的东西,但没有运气..我读了一些帖子,他们建议它的 cz 我重用细胞..请建议!
-
欢迎来到 Stack Overflow (SO)!当有人帮助您时,请接受答案,以便显示正确答案并奖励他们的帮助以增加声誉!
标签: ios uitableview uiswitch