私はこれを長年検索してきましたが、明確な答えはありません。UISwitchスクロール時のリセットテーブルビュー
私はテーブルビューのセルにaccessoryView
としてUISwitch
を持っています。問題は、テーブルをスクロールするたびにスイッチが元の状態にリセットされることです。
はここに私の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;
}
ところで、私は、ヘッダファイルに私のスイッチを宣言し、プロパティとして設定し、それを合成してきました。
**スイッチが画面*をスクロールしてから再びオンにするたびにオンに戻り、オンに戻り、少しスクロールしても変更されることを意味しますか? – lnafziger
スイッチが画面をスクロールすると、それが変わります... – GSethi
これは私がコードに基づいて考えたものです。これは画面に表示されるたびに新しいスイッチになるためです。 – lnafziger