如何在TableView
中放置开关?我有一个数组设置为TableView
有三个项目,我想在每个项目旁边显示一个开关。这可能吗?我已经尝试将其嵌入TableView
单元格中,但根本没有做任何事情,老实说我不知道为什么。任何帮助表示赞赏。
顺便说一句,这是我使用的代码没有帮助:
Cell.accessoryView = switchView;
[switchView setOn:NO animated:NO];
[switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[switchView release];
答案 0 :(得分:-1)
您可以通过将开关置于UITableViewCell
轻松完成。我做到了没有任何问题。我正在分享我的cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
StatusCell *cell = (StatusCell*) [self.tableView dequeueReusableCellWithIdentifier:@"StatusCell"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StatusCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.name.text = [_StatArr objectAtIndex:indexPath.row];
cell.switchControl.tag = indexPath.row;
if ([[_StatArr objectAtIndex:indexPath.row] isEqualToString:@"Seat Allocated"]) {
NSString *s = [[_StatArr objectAtIndex:indexPath.row] lowercaseString];
NSString *ss = [s stringByReplacingOccurrencesOfString:@" " withString:@"_"];
[cell.switchControl setOn:[[self.statusArr objectForKey:ss] boolValue] animated:YES];
}
else{
if ([[_StatArr objectAtIndex:indexPath.row] isEqualToString:@"Seated"]) {
[cell.switchControl setOn:[[self.statusArr objectForKey:[[_StatArr objectAtIndex:indexPath.row] lowercaseString]] boolValue] animated:YES];
}
else
[cell.switchControl setOn:[[self.statusArr objectForKey:[[_StatArr objectAtIndex:indexPath.row] lowercaseString]] boolValue] animated:YES];
}
[cell.switchControl addTarget:self action:@selector(stateSwitched:) forControlEvents:UIControlEventValueChanged];
return cell;
}
您只需要跟踪每个开关状态。希望这有助于.. :))