我在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;
}
}
}
}
答案 0 :(得分:0)
第一件事:每次加载单元格时都不应该添加UIButton。您应该在if (cell == nil) block
内添加它们。
下一步:您应该在一个单独的数组( NSMutableArray )中跟踪所选的单选按钮索引( int 值) ,并在if (cell == nil) block
之外选择相应的UIButton。
我希望 this tutorial 能为您提供帮助。