gpt4 book ai didi

ios - 带有 UISwitch 的 UITableViewCell 不会重绘?

转载 作者:行者123 更新时间:2023-12-01 16:36:38 25 4
gpt4 key购买 nike

我有一个 uitableviewcell,其中包含一个 uiswitch(obj-c)。开关工作正常,但是当点击表格 View 外的按钮时,我需要单元格中的 uiswitch 重绘并使其禁用。我正在尝试以下方法,但它不起作用?我的 CellForRowAtIndex 被调用,我的代码块被调用,说禁用 UISwitch 但没有任何 react ?

- (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 并使用委托(delegate),我成功地实现了开关单元。

@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

希望能给你一些方向。

关于ios - 带有 UISwitch 的 UITableViewCell 不会重绘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27625663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com