UITableViewCell
안에 내 UISwitch
에 문제가 있습니다. 한 스위치의 값을 변경 한 다음 위로 또는 아래로 스크롤하면 모든 스위치가 엉망입니다. 배열을 사용하여 재사용 성으로 인해 각 스위치의 상태를 저장할 때마다 매번 엉망이됩니다.위 또는 아래로 스크롤 할 때 uitableviewcell의 Uiswitch가 재설정됩니까?
-(void)switchChanged:(UISwitch *)sender
{
UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *index=[mainTableView indexPathForCell:cell];
if (sender.on)
{
[[self.SwitchArray objectAtIndex:index.section] replaceObjectAtIndex:index.row withObject:@"ON"];
NSString *word= [[self.mainArray objectAtIndex:index.section ] objectAtIndex:index.row];
}
else
{
//call the first array by section
[[self.SwitchArray objectAtIndex:index.section] replaceObjectAtIndex:index.row withObject:@"OFF"];
NSString *word= [[self.mainArray objectAtIndex:index.section ] objectAtIndex:index.row];
}
[padFactoids setObject:[NSKeyedArchiver archivedDataWithRootObject:SwitchArray] forKey:@"savedArray"];
[padFactoids synchronize];
}
내가 너무 많은 도움을 주셔서 감사합니다 :
여기- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"Cell"];
}
UISwitch *switchController = [[UISwitch alloc] initWithFrame:CGRectZero];
CGRect switchFrame = switchController.frame;
[switchController setOn:YES animated:NO];
//set its x and y value, this you will have to determine how to space it on the left side
switchFrame.origin.x = 50.0f;
switchFrame.origin.y = 10.0f;
switchController.frame = switchFrame;
[switchController addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[cell addSubview:switchController ];
UILabel *label ;
label=(UILabel *)[cell viewWithTag:1];
NSString *value = [[mainArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
label.text = value;
label.textAlignment = NSTextAlignmentRight;
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
//This for persist switch state when scroll up or down
if ([[[self.SwitchArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row ]isEqualToString:@"ON"])
{
switchController.on=YES;
}
else
{
switchController.on=NO;
}
return cell;
}
이 SwitchChanged 이벤트 :
여기 cellForRowAtIndexPath
방법이다.
중단 점을 설정하고'self.SwitchArray'를보고 그것이 존재하는지 ('nil'이 아니라는) 확인하고 예상 한 데이터를 포함하는지보십시오. 짐작할 수 있듯이, 여러분의 배열은'nil'이므로 항상 꺼져있는 스위치가 돌아옵니다. 귀하의 답변에 대해 당신보다 @@@ – DBD
입니다. 하지만 모든 스위치가 꺼져있는 것은 아닙니다. 나는 테이블 뷰의 스위치 중 하나를 바꿀 때마다 위 또는 아래로 스크롤하여 스위치가 켜져 있고 부통령이 될 때까지 엉망이된다. – user2323070
ChrisH가 언급했듯이, 스위치가 이미 존재하는 경우에도 셀을 구성 할 때마다 새로운 'UISwitch'를 생성하게됩니다. 그게 바로 고쳐야 할 것이지만, 데이터 모델을 확인해야 할 필요가 있다고 생각합니다. 나는 당신의 가치가 자신이 생각하는 것처럼 절약되고 있다고 확신하지 않기 때문입니다. – DBD