Ios UITableViewCell和UISwitch赢得';你不重画吗?
我有一个uitableviewcell,其中包含一个uiswitch(obj-c)。该开关工作正常,但当单击tableview外的按钮时,我需要单元格中的uiswitch重新绘制并将其禁用。我正在尝试以下方法,但不起作用?调用了我的CellForRowatinex,调用了我的代码块,上面写着禁用UISwitch,但什么也没发生Ios UITableViewCell和UISwitch赢得';你不重画吗?,ios,objective-c,uitableview,uiswitch,Ios,Objective C,Uitableview,Uiswitch,我有一个uitableviewcell,其中包含一个uiswitch(obj-c)。该开关工作正常,但当单击tableview外的按钮时,我需要单元格中的uiswitch重新绘制并将其禁用。我正在尝试以下方法,但不起作用?调用了我的CellForRowatinex,调用了我的代码块,上面写着禁用UISwitch,但什么也没发生 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSInde
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
thingic NSString *cellIdentifier = @"Cell";
NSString *danID = [[NSUserDefaults standardUserDefaults]objectForKey:kSavingdanID];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:cellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:21];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
cell.selectedBackgroundView = [[UIView alloc] init];
}
if(indexPath.row == 0) { // Mute thing Calls
cell.textLabel.text = @"Public Calls";
self.thingSwitchView = [[UISwitch alloc] initWithFrame:CGRectMake(170, 13, 0, 0)];
[cell.contentView addSubview:self.thingSwitchView];
// cell.accessoryView = switchView;
[self.thingSwitchView setOn:YES animated:NO];
[self.thingSwitchView addTarget:self action:@selector(thingSwitchChanged:) forControlEvents:UIControlEventValueChanged];
cell.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor colorWithRed: 0.0/255.0 green: 0.0/255.0 blue: 0.0/255.0 alpha: 0.4];
} else if(indexPath.row == 1) { // Mute boo Calls
cell.textLabel.text = @"boo Calls";
self.booSwitchView = [[UISwitch alloc] initWithFrame:CGRectMake(170, 12, 0, 0)];
[cell.contentView addSubview:self.booSwitchView];
// cell.accessoryView = switchView;
[self.booSwitchView setOn:YES animated:NO];
[self.booSwitchView addTarget:self action:@selector(booSwitchChanged:) forControlEvents:UIControlEventValueChanged];
cell.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor colorWithRed: 0.0/255.0 green: 0.0/255.0 blue: 0.0/255.0 alpha: 0.4];
} else {
// do nothing
}
if([NSString isEmpty:danID]){
[self.thingSwitchView setEnabled:NO];
[self.booSwitchView setEnabled:NO];
[cell.backgroundView setNeedsDisplay];
[cell setNeedsDisplay];
}
return cell;
}
通过对UITableViewCell进行子类化并使用委托,我成功地实现了开关单元
@class EGSwitchCell;
@protocol EGSwitchCellDelegate <NSObject>
-(void)switchCellDidChange:(EGSwitchCell *)switchCell;
@end
@interface EGSwitchCell : UITableViewCell
@property (nonatomic, weak) IBOutlet UISwitch *mySwitch;
@property (weak, nonatomic) IBOutlet UILabel *labelTitle;
@property (nonatomic, weak) id<EGSwitchCellDelegate> delegate;
-(IBAction)switchChanged:(id)sender;
@end
@class-EGSwitchCell;
@协议代理
-(void)switchCellDidChange:(EGSwitchCell*)switchCell;
@结束
@接口EGSwitchCell:UITableViewCell
@属性(非原子,弱)IBUISwitch*mySwitch;
@属性(弱,非原子)IBUILabel*标签;
@属性(非原子,弱)id委托;
-(iAction)开关已更改:(id)发送方;
@结束
希望它能给你一些指导。首先,在每次调用
cellForRowAtIndexPath:
的过程中,添加新的开关作为子视图,你在旧开关的基础上堆积新开关。我建议您在创建开关时将其添加到相应的单元格中。同样,我还建议您在创建单元格时设置单元格的contentView.backgroundColor,因为它不会更改
这花了我一点时间,但我想我明白了为什么你要用开关类变量。。。我假设您的表(?)中只有一个部分,因此这些开关每次只出现一次。如果是这样的话,我们可以利用这些类变量,因为即使开关改变,它们也可以维护它们的变量
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
thingic NSString *cellIdentifier = @"Cell";
NSString *danID = [[NSUserDefaults standardUserDefaults]objectForKey:kSavingdanID];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:cellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:21];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
cell.selectedBackgroundView = [[UIView alloc] init];
if (indexPath.row == 0) {
self.thingSwitchView = [[UISwitch alloc] initWithFrame:CGRectMake(170, 13, 0, 0)];
[self.thingSwitchView setOn:YES animated:NO];
[self.thingSwitchView addTarget:self action:@selector(thingSwitchChanged:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:self.thingSwitchView];
cell.contentView.backgroundColor = [UIColor colorWithRed: 0.0/255.0 green: 0.0/255.0 blue: 0.0/255.0 alpha: 0.4];
} else if (indexPath.row == 1) {
self.booSwitchView = [[UISwitch alloc] initWithFrame:CGRectMake(170, 12, 0, 0)];
[self.booSwitchView setOn:YES animated:NO];
[self.booSwitchView addTarget:self action:@selector(booSwitchChanged:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:self.booSwitchView];
cell.contentView.backgroundColor = [UIColor colorWithRed: 0.0/255.0 green: 0.0/255.0 blue: 0.0/255.0 alpha: 0.4];
}
}
if (indexPath.row == 0) { // Mute thing Calls
cell.textLabel.text = @"Public Calls";
} else if (indexPath.row == 1) { // Mute boo Calls
cell.textLabel.text = @"boo Calls";
}
if ([NSString isEmpty:danID]) {
[self.thingSwitchView setEnabled:NO];
[self.booSwitchView setEnabled:NO];
} else {
[self.thingSwitchView setEnabled:YES];
[self.booSwitchView setEnabled:YES];
}
return cell;
}
另外,请确保您正在按预期更改
NSUserDefaults
objectForKeykSavingdanID
。如果您仍然存在问题,我建议从thingSwitchChanged:
和booSwitchChanged:
发布您的代码。您不能以当前方式执行此操作。你不能用self在细胞内获得开关。
Self.thingSwitchView
和Self.booSwitchView
包含最后分配的交换机。这不是牢房里的开关。此外,即使单元被重用,您每次都会创建一个新的交换机。这也不是一个好的做法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
thingic NSString *cellIdentifier = @"Cell";
NSString *danID = [[NSUserDefaults standardUserDefaults]objectForKey:kSavingdanID];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:cellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:21];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
cell.selectedBackgroundView = [[UIView alloc] init];
}
if(indexPath.row == 0 && ![cell.contentView viewWithTag:101])
{ // Mute thing Calls
cell.textLabel.text = @"Public Calls";
self.thingSwitchView = [[UISwitch alloc] initWithFrame:CGRectMake(170, 13, 0, 0)];
self.thingSwitchView.tag = 101;
[cell.contentView addSubview:self.thingSwitchView];
[self.thingSwitchView setOn:YES animated:NO];
[self.thingSwitchView addTarget:self action:@selector(thingSwitchChanged:) forControlEvents:UIControlEventValueChanged];
cell.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor colorWithRed: 0.0/255.0 green: 0.0/255.0 blue: 0.0/255.0 alpha: 0.4];
}
else if(indexPath.row == 1 && ![cell.contentView viewWithTag:102])
{ // Mute boo Calls
cell.textLabel.text = @"boo Calls";
self.booSwitchView = [[UISwitch alloc] initWithFrame:CGRectMake(170, 12, 0, 0)];
[cell.contentView addSubview:self.booSwitchView];
self.booSwitchView.tag = 102;
[self.booSwitchView setOn:YES animated:NO];
[self.booSwitchView addTarget:self action:@selector(booSwitchChanged:) forControlEvents:UIControlEventValueChanged];
cell.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor colorWithRed: 0.0/255.0 green: 0.0/255.0 blue: 0.0/255.0 alpha: 0.4];
}
UISwitch *thingSwitch = [cell.contentView viewWithTag:101];
UISwitch *booSwitch = [cell.contentView viewWithTag:102];
if([NSString isEmpty:danID])
{
[thingSwitch setEnabled:NO];
[booSwitch setEnabled:NO];
}
else
{
[thingSwitch setEnabled:YES];
[booSwitch setEnabled:YES];
}
return cell;
}
不要介意那些其他的评论(已删除)。。。我看到了将开关添加为子视图的位置。但是你应该(1)重用这些开关,(2)在本地声明它们。在什么情况下你想关闭开关?您还没有在
cellforrowatinexpath
中说明这一点。我正准备写一个答案,但有太多的未知数。你需要发布更多的代码。特别是从thingSwitchChanged开始:但是像您那样内联操作如何在其他代码中访问交换机,因为我不再有引用?@jdog:为什么您要用其他方法访问它,您如何访问它?