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

标签 ios objective-c uitableview uiswitch

我有一个 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/

相关文章:

ios - 浏览器中的 HTML5 可以在 iOS 锁屏上连续播放音频吗?

iphone - 检测 iOS 应用的 AppStore 安装

objective-c - 在 NSDocument 上显示进度指示器表显示为灰色(尽管仍然有用)

ios - iPad 详细 View 设计 : How prevent the TableViewController header and toolbar to scroll out of the view?

ios - Swift - 使用 Segue 将数据从容器传递到 TableViewController

iphone - “MyAnnotation”可能无法响应 'initWithDictionary:'

ios - Facebook 和 G+ 使用 UIWebView 登录

ios - 无法在表格 View 单元格中容纳多行

ios - DZNEmptyDataSet 与 RxSwift 中的 tableview 绑定(bind)不兼容。有人能够使它工作吗?

objective-c - 如何在 Objective-C 中将变量添加到类别?