根据 UISwitch 的状态删除或添加 UITableViewCell
我有一张四行的桌子。其中两个应该首先显示——第 1 行和第 2 行。另外两个应该仅在位于第 2 行的 UISwitch 的状态为“On”时出现。如果状态设置为“关闭”,那么它们应该消失。
我创建了一个包含 UISwitch
的 UITableViewCell
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSMutableArray *dict = [dictionary objectForKey:@"DATA"];
//the location of the switch!
if(indexPath.row == 1){
SwitchCell *cell = (SwitchCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SwitchCell" owner:self options:nil];
cell = (SwitchCell *)[nib objectAtIndex:0];
}
cell.title1.text = [dict objectAtIndex:indexPath.row];
[cell.switchSpecifyEnd addTarget:self action:@selector(switchSpecifyEnd) forControlEvents:UIControlEventValueChanged];
mySwitch = cell.switchSpecifyEnd;
return cell;
}else{
static NSString *CellIdentifier1 = @"CustonCell";
DateTimeCell *cell = (DateTimeCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DateTimeCell" owner:self options:nil];
cell = (DateTimeCell *)[nib objectAtIndex:0];
}
cell.description.text = [dict objectAtIndex:indexPath.row];
cell.dateTime.text = self.date;
return cell;
}
}
-(void) switchSpecifyEnd{
//displays only 0 (defined initial state of my switch)
NSLog(@"SWITCH - Specify End: %d", mySwitch.state);
// [tableView1 deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// [tableView1 reloadData];
}
如何让第 3 行和第 4 行最初不显示,并在开关状态更改为“时让它们显示”在”?
I have a table with four rows. Two of them should be displayed initially -- rows 1 and 2. The other two should only appear if the state of a UISwitch
, which is located in row 2, is "On". If the state is set to "Off", then they should disappear.
I have created a UITableViewCell
with holds the UISwitch
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSMutableArray *dict = [dictionary objectForKey:@"DATA"];
//the location of the switch!
if(indexPath.row == 1){
SwitchCell *cell = (SwitchCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SwitchCell" owner:self options:nil];
cell = (SwitchCell *)[nib objectAtIndex:0];
}
cell.title1.text = [dict objectAtIndex:indexPath.row];
[cell.switchSpecifyEnd addTarget:self action:@selector(switchSpecifyEnd) forControlEvents:UIControlEventValueChanged];
mySwitch = cell.switchSpecifyEnd;
return cell;
}else{
static NSString *CellIdentifier1 = @"CustonCell";
DateTimeCell *cell = (DateTimeCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DateTimeCell" owner:self options:nil];
cell = (DateTimeCell *)[nib objectAtIndex:0];
}
cell.description.text = [dict objectAtIndex:indexPath.row];
cell.dateTime.text = self.date;
return cell;
}
}
-(void) switchSpecifyEnd{
//displays only 0 (defined initial state of my switch)
NSLog(@"SWITCH - Specify End: %d", mySwitch.state);
// [tableView1 deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// [tableView1 reloadData];
}
How can I get rows 3 and 4 to not be displayed initially, and let them appear if the switch's state is changed to "On"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为最好的做法是:
UISwitch
移出 tableView,只需将其放在表格上方NSMutableArray
作为数据源只需添加或删除将两个元素传入或传出数据源数组,并在切换开关后调用
[tableView reloadData]
。这意味着,您不仅要使 tableViewCell 不可见,还要在每次想要隐藏它时删除其后面的整个数据。这可能不太理想,因此您也可以选择使用
NSArray
来保存数据。您应该有一个全局BOOL
变量来跟踪是否应显示 2 个单元格,而不是从中删除/添加数据。在UITableViewDataSource
方法中,将函数的内容包装在if
子句中。例如对于- (NSInteger)tableView:numberOfRowsInSection:
那么您应该在
- (UITableViewCell*)tableView:cellForRowAtIndexPath:
方法中执行相同的操作来配置单元格。希望这有帮助!
I think the best practice for this would be to:
UISwitch
out of the tableView, just put it above the tableNSMutableArray
as data sourceJust add or remove the two elements to or from the data source array and call
[tableView reloadData]
after switching the switch.This implies that, instead of just making the tableViewCell invisible, you rather remove the entire data behind it every time you want to hide it. This can be less ideal, so you could also opt for an
NSArray
for holding your data. Instead of then removing/adding the data from/to it, you should have a globalBOOL
variable to keep track of whether or not the 2 cells should be shown. In yourUITableViewDataSource
methods, wrap the contents of the functions in anif
clause. For example for- (NSInteger)tableView:numberOfRowsInSection:
Then you should do the same in the
- (UITableViewCell*)tableView:cellForRowAtIndexPath:
method to configure your cells.Hope this helps!
您需要在类中设置一个变量来指示开关是打开还是关闭。在 tableView:numberOfRowsInSection 中使用该变量来确定是否需要返回 2 还是 4 作为单元格数。
将开关切换到“On”后,您需要更改该变量并调用 reloadData(您的开关操作中已经有此调用,但它已被注释掉)。
然后表视图控制器将再次调用 tableView:numberOfRowsInSection 并获取新的单元格数量。然后 tableView:cellForRowAtIndexPath: 将再次被调用并再次请求所有单元格,现在包括数字 3 和 4。
You need to set a variable in your class that indicates if the switch is on or of. Use that variable in tableView:numberOfRowsInSection to determine if you need to return 2 or 4 as the number of cells.
After you flipped the switch to "On", you need to change that variable and call reloadData (you already have this call in your switch action but it is commented out).
The table view controller will then call tableView:numberOfRowsInSection again and get the new number of cells. Then tableView:cellForRowAtIndexPath: will be called again and ask for all the cells again, now including number 3 and 4.