我正在制作电话簿。
我正在尝试创建一个UITableView,其中包含一个原型单元,其中包含一个UISwitch,用于描述特定联系人是公共联系人还是私人联系人。
如何创建这个以便UISwitch在滚动期间保持其切换状态? dequeueReusableCellWithIdentifier
给了我很多问题,因为它没有保存UISwitch的转换状态。
虽然我喜欢节省内存,但是在cellForRowAtIndexPath中切换交换机(根据联系人的隐私布尔值)无法始终显示联系人的公共/私人状态。
请参阅以下截图我正在尝试构建的内容:
编辑:这是我的cellForRowAtIndexPath:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AHBContact *thisContact = nil;
AHBBrowseContactCell *cell =[tableView dequeueReusableCellWithIdentifier:@"BROWSECELL"];
// from contacts list
thisContact = [self.contacts[indexPath.section] allValues][0][indexPath.row];
cell.customPublicSwitch.onTintColor = [AHBUtilities greenColor];
cell.customPublicSwitch.onText= @"Public";
cell.customPublicSwitch.offText=@"Private";
if(thisContact.privacy){
NSLog(@"public %@",thisContact.fullName);
cell.customPublicSwitch.on = YES;
}
else{
NSLog(@"private %@", thisContact.fullName);
cell.customPublicSwitch.on = NO;
}
cell.labelName.text = thisContact.fullName;
cell.labelName.font = [AHBUtilities regularFontWithSize:cell.labelName.font.pointSize];
cell.imageViewIcon.image = [AHBIcons phoneIconForCategory:thisContact.category];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.backgroundColor = [UIColor colorWithWhite:1 alpha:0.55];
return cell;
}
第二次编辑:这是我的“togglePublic”方法,它在交换机的“valueChanged”上运行
-(IBAction)togglePublic:(UIControl *)button withEvent:(UIEvent *)event {
DCRoundSwitch *switch1 = (DCRoundSwitch *)button;
UITableViewCell *cell = (UITableViewCell *)switch1.superview;
NSIndexPath *indexPath = [self.tableview indexPathForCell: cell.superview];
if ( indexPath == nil ){
return;
}
AHBContact *contact = [self.contacts[indexPath.section] allValues][0][indexPath.row];
contact.privacy = !contact.privacy;
NSLog(@"Public switch toggled for: %@", contact.firstName);
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[[AHBContactsController sharedController] updateContact:contact completion:^(id result) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSLog (@"Public/Private updated%@",result);
} failure:^(NSError *error) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Sync Problem"
message:@"Could save changes. Please try again."
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[message show];
}];
}
答案 0 :(得分:0)
尝试在委托方法中设置
- (void)tableView:(UITableView *)tableView willDsiplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
UISwitch *switch = cell.yourCellSwitch;
switch.on = yourSwitchVariable;
}
答案 1 :(得分:0)
您在 cellForRowAtIndexPath:中创建的开关可能就是问题所在。尝试这样做。使用UILabel和UISwitch创建自定义单元格。制作这些IBOutlets和自定义单元格的属性并连接它们。现在在cellForRowAtIndexPath:方法中使用自定义单元格而不是默认单元格。当单元格被重用时,每当一个单元格在屏幕上可见时,就会调用此方法,我猜每次创建UISwitch然后分配其状态时会出现不一致
static NSString *cellIdentifier = @"CellIdentifier";
YourCustomCell *cell = (YourCustomCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"YourCustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (YourCustomCell *) currentObject;
break;
}
}
}
// from contacts list
thisContact = [self.contacts[indexPath.section] allValues][0][indexPath.row];
if(thisContact.privacy){
NSLog(@"public %@",thisContact.fullName);
cell.customPublicSwitch.on = YES;
}
else{
NSLog(@"private %@", thisContact.fullName);
cell.customPublicSwitch.on = NO;
}
cell.labelName.text = thisContact.fullName;
cell.labelName.font = [AHBUtilities regularFontWithSize:cell.labelName.font.pointSize];
cell.imageViewIcon.image = [AHBIcons phoneIconForCategory:thisContact.category];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.backgroundColor = [UIColor colorWithWhite:1 alpha:0.55];
return cell;
}
希望这会有所帮助:)