일부 데이터로 UITable을 만들었으며 UISwitch가 포함되어 있지만 실행시 테이블에 표시되지 않았습니다.UISwitch가 UITable에 표시되지 않음
.H@interface CalcViewController : UITableViewController {
IBOutlet UITableView *mainTableView;
NSMutableArray *courseMURArray;
NSMutableArray *switchStates;
}
및하는 .m
- (void)viewDidLoad
{
[super viewDidLoad];
switchStates = [[NSMutableArray alloc] init ];
int i = 0;
for (i = 0; i < 33; i++) {
[switchStates addObject:@"OFF"];
}
}
및
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
UISwitch *theSwitch = nil;
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
theSwitch = [[UISwitch alloc]initWithFrame:CGRectZero];
theSwitch.tag = 100;
CGRect frame = theSwitch.frame;
frame.origin.x = 230;
frame.origin.y = 8;
theSwitch.frame = frame;
[theSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:theSwitch];
}else{
theSwitch = [cell.contentView viewWithTag:100];
}
if ([[switchStates objectAtIndex:indexPath.row] isEqualToString:@"ON"]) {
theSwitch.on = YES;
}else{
theSwitch.on = NO;
}
return cell;
}
에서의
이는 선택 방법
-(void) switchChanged: (UISwitch *) sender{
UITableViewCell *theParentCell = [[ sender superview] superview];
NSIndexPath * indexPathOfSwitch = [mainTableView indexPathForCell:theParentCell];
if (sender.on) {
[switchStates replaceObjectAtIndex:indexPathOfSwitch.row withObject:@"ON"];
}else{
[switchStates replaceObjectAtIndex:indexPathOfSwitch.row withObject:@"OFF"];
}
}
모든 것이 데이터와 잘 맞지만 문제가 있습니까?
버튼을 테이블 뷰 셀의 'accessoryView'로 만드는 것은 어떨까요? 그것은 훨씬 깨끗한 해결책입니다. –
무슨 뜻인가요? – NamshanNet
나는'cell.accessoryView = theSwitch; '를 의미한다. –