我有一個包含uiswitch(obj-c)的uitableviewcell。該開關工作正常,但是當點擊tableview外的按鈕時,我需要單元格中的uiswitch重新繪製並禁用它。我正在嘗試以下,但它不會工作?我的CellForRowAtIndex被調用,我的代碼塊被稱爲禁用UISwitch,但沒有任何反應?帶有UISwitch的UITableViewCell不會重繪?
- (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;
}
沒關係那些其他評論(刪除)...我看到你在哪裏添加開關作爲子視圖。但是您應該(1)重用這些開關並(2)在本地聲明它們。 –
你想在什麼情況下關閉開關?你沒有在'cellForRowAtIndexPath'中說明。我開始寫出答案,但有太多未知數。你需要發佈更多的代碼。特別是從thingSwitchChanged: –