1
我使用UITableView單元格上的單選按鈕。首先單選按鈕未選中。當單選按鈕被檢查時。我正在滾動UITableView單選按鈕未選中。使用此代碼如下當單選按鈕選擇檢查
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
static NSString *CellIdentifierFirst = @"CellFirst";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierFirst];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil] autorelease];
}
NSArray *cellSubs = cell.contentView.subviews;
for (int i = 0 ; i < [cellSubs count] ; i++) {
[[cellSubs objectAtIndex:i] removeFromSuperview];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIButton *redioBtn1 = [[UIButton alloc]initWithFrame:CGRectMake(20, 20, 20, 20)];
[redioBtn1 setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];
[redioBtn1 addTarget:self action:@selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
redioBtn1.tag = ++tagCount;
[cell.contentView addSubview:redioBtn1];
UIButton *redioBtn2 = [[UIButton alloc]initWithFrame:CGRectMake(80, 20, 20, 20)];
[redioBtn2 setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];
[redioBtn2 addTarget:self action:@selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
redioBtn2.tag = ++tagCount;
[cell.contentView addSubview:redioBtn2];
UIButton *redioBtn3 = [[UIButton alloc]initWithFrame:CGRectMake(140, 20, 20, 20)];
[redioBtn3 setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];
[redioBtn3 addTarget:self action:@selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
redioBtn3.tag = ++tagCount;
[cell.contentView addSubview:redioBtn3];
return cell;
}
-(void)selectRadioButon:(id)sender {
btnTag = [sender tag];
NSArray *arr = self.view.subviews;
UITableView *tblCell = [arr objectAtIndex:0];
NSArray *cellAry = tblCell.subviews;
for (int i = 0; i <[cellAry count]; i++) {
UITableViewCell *content = [cellAry objectAtIndex:i];
NSArray *contentArry = content.contentView.subviews;
for (int j = 0; j <[contentArry count]; j++) {
UIButton *button = [contentArry objectAtIndex:j];
if (btnTag == button.tag) {
for (int i = 0; i <[contentArry count]; i++) {
UIButton *button = [contentArry objectAtIndex:i];
if (btnTag == button.tag) {
if ([sender currentImage]==[UIImage imageNamed:@"radioselect.png"])
[button setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];
else
[button setImage:[UIImage imageNamed:@"radioselect.png"] forState:UIControlStateNormal];
}
else
[button setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];
}
return;
}
}
}
}