在自定义 UITableViewCell 中控制 UISwitch [英] Controlling UISwitch in a custom UITableViewCell
问题描述
我为单元创建了一个自定义 UITableViewCell
和一个类.然后我将 viewController 中的这个单元格加载到 tableview 中.自定义单元格有一个标签和一个 UiSwitch.我设法让开关按照我的意愿工作,并更改了需要更改的值.
I have created a custom UITableViewCell
and a class for the cell. I then load this cell in my viewController into a tableview. The custom cell has a label and a UiSwitch. I have managed to get the switch to work as I want it to and change values that are needed to change.
我现在想要在 UITableView
之外有一个单独的 UISwitch - 当它打开时 - UITableView 中的所有开关都打开.
I now wanted a separate UISwitch outside of the UITableView
- that when it is turned on - all the switches in the UITableView are turned on.
由于某种原因,我无法使其正常工作.
For some reason i cannot get this to work.
这是自定义单元格的代码:
This is the code for the custom cell:
- (void)selectAllSwitches:(BOOL)selected
{
if (selected){
[self.selectSwitch setOn:YES animated:YES];
}else{
[self.selectSwitch setOn:NO animated:YES];
}
}
-(void)setMaillist:(BBMailList *)aMaillist
{
maillist = aMaillist;
if (maillist){
self.nameLabel.text = maillist.name;
if (self.maillist.isSubscribed){
self.selectSwitch.on = YES;
}else{
self.selectSwitch.on = NO;
}
}
}
- (IBAction)switchControl:(UISwitch *)sender
{
self.maillist.isSubscribed = !self.maillist.isSubscribed;
NSLog (@"IsSubScribed: %ul", self.maillist.isSubscribed);
if (self.maillist.isSubscribed){
self.selectSwitch.on = YES;
}else{
self.selectSwitch.on = NO;
}
}
然后在 cellForRowAtIndexPath 中:
Then in cellForRowAtIndexPath:
static NSString *mailListTableViewCellIdentifier = @"BBMailListTableViewCell";
if (tableView == self.maillistTableview ){
BBMailListTableViewCell * maillistTableviewCell = [self.maillistTableview dequeueReusableCellWithIdentifier:mailListTableViewCellIdentifier];
if (!maillistTableviewCell){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:mailListTableViewCellIdentifier owner:self options:nil];
maillistTableviewCell = nib[0];
}
if ([self.mailLists count ] > 0){
NSMutableArray *newArray = [NSMutableArray arrayWithArray:self.mailLists];
[newArray removeLastObject];
[newArray removeLastObject];
[self.maillistTableview setContentSize:CGSizeMake(320, 400)];
BBMailList *maillist = newArray[indexPath.row];
maillistTableviewCell.maillist = maillist;
}
return maillistTableviewCell;
}
此代码按我的需要工作.
This code works as I need it to.
然后我在我的 viewController 中为 tableView 之外的 UISwitch 创建了一个 IBAction 方法
I then created an IBAction methon in my viewController for the UISwitch that is outside of the tableView
- (IBAction)selectAllSwitch:(UISwitch *)sender
{
BBMailListTableViewCell *switchButton = [[BBMailListTableViewCell alloc]init];
[switchButton selectAllSwitches:YES];
}
这在我的 UITableViewCell 类中运行了正确的方法 - 但 UITableView 不反映更改.我添加了 [self.myTableView reloadData];
- 但这并没有解决问题.有人能为我阐明一些不是他的光吗?
This runs the correct method in my UITableViewCell class - but the UITableView doesn't reflect the changes. I added [self.myTableView reloadData];
- but this did not solve the issue. Can anyone shed some light not his for me?
****更新****
更新了 UITableView 之外的 UISwitch 代码
Updated Code for the UISwitch outside of the UITableView
- (IBAction)selectAllSwitch:(UISwitch *)sender
{
BBMailList *tempMailList = [[BBMailList alloc]init];
tempMailList.isSubscribed = YES;
[self.maillistTableViewCell setMaillist:tempMailList];
[self.maillistTableViewCell selectAllSwitches:YES];
[self.maillistTableview reloadData];
}
每个 mailistItem 都是我用来存储数据的 NSObject.上面的代码运行了正确的方法 - 但似乎我没有保持状态,当 [self.mayTableView reloadData];运行 - 它将 tableview 数据重置为原始状态?
Each mailistItem is a NSObject I use to store data with. The above code runs the correct methods - but it seems I am not keeping the state maybe and when [self.mayTableView reloadData]; runs - it resets the tableview data to original state?
推荐答案
解决问题的逻辑是:
1) 从您的代码看来,开/关功能取决于您的 maillist.isSubscribed
属性.在视图控制器的 IBAction
中,将其设置为 true
.
1) From your code it looks that the on/off functionality is dependent on your maillist.isSubscribed
property. In your IBAction
of viewcontroller, set it to true
.
2) 在此 IBAction 中调用 [self.myTableView reloadData];
.因此,一旦表格加载单元格,数组元素将帮助开关设置为 ON/OFF.
2) Call [self.myTableView reloadData];
in this IBAction. So once the table loads cell the array's element will help the switch set to ON/OFF.
希望有帮助!
编辑
您好像迷失了方向.因为在 viewController 的 IBAction 中,您正在创建一个 BBMailList
类型的新数组.试试这个:
It seems like you are losing your track. Because in your IBAction of your viewController you are creating a new array of type BBMailList
. Try like this:
假设您的 self.mailLists
是 BBMailList
类型的集合.
Assuming your self.mailLists
is a collection of BBMailList
type.
现在:
- (IBAction)selectAllSwitch:(UISwitch *)sender
{
// loop through each array element set it to true.
for (BBMailList *tempMailList in self.mailLists)
{
tempMailList.isSubscribed = Yes;
}
// reload table data so once table
[self.maillistTableview reloadData];
}
并在 cellForRowAtIndexPath
中更改您的邮件列表设置,如下所示:
and in cellForRowAtIndexPath
change your setting up of mail list like this:
if ([self.mailLists count ] > 0){
NSMutableArray *newArray = [NSMutableArray arrayWithArray:self.mailLists];
[newArray removeLastObject];
[newArray removeLastObject];
[self.maillistTableview setContentSize:CGSizeMake(320, 400)];
BBMailList *maillist = newArray[indexPath.row];
[maillistTableviewCell setMaillist: maillist]; // <--- Changed this line
}
这篇关于在自定义 UITableViewCell 中控制 UISwitch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!