Iphone 当单选按钮选择check(检查)时
我正在使用UITableView单元格上的单选按钮。首先,取消选中单选按钮。当单选按钮处于选中状态时。我正在滚动UITableView单选按钮未选中。下面是使用此代码的步骤Iphone 当单选按钮选择check(检查)时,iphone,objective-c,ios,ios4,radio-button,Iphone,Objective C,Ios,Ios4,Radio Button,我正在使用UITableView单元格上的单选按钮。首先,取消选中单选按钮。当单选按钮处于选中状态时。我正在滚动UITableView单选按钮未选中。下面是使用此代码的步骤 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteg
- (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;
}
}
}
}
-(NSInteger)节数表视图:(UITableView*)表视图{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回100;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
静态NSString*CellIdentifierFirst=@“CellFirst”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifierFirst];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2重用标识符:CellIdentifier]自动释放];
//cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1重用标识符:nil]自动释放];
}
NSArray*cellSubs=cell.contentView.subview;
对于(int i=0;i<[cellSubs计数];i++){
[[cellSubs objectAtIndex:i]从SuperView中移除];
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UIButton*rediobn1=[[UIButton alloc]initWithFrame:CGRectMake(20,20,20,20)];
[redioBtn1 setImage:[UIImage IMAGENAME:@“radiounselect.png”]用于状态:UIControlStateNormal];
[rediobn1 addTarget:self action:@selector(selectRadioButon:)for controlEvents:UIControlEventTouchUpInside];
rediobn1.tag=++tagCount;
[cell.contentView addSubview:rediobn1];
UIButton*redioBtn2=[[UIButton alloc]initWithFrame:CGRectMake(80,20,20,20)];
[redioBtn2 setImage:[UIImage IMAGENAME:@“radiounselect.png”]用于状态:UIControlStateNormal];
[redioBtn2添加目标:自我操作:@选择器(selectRadioButon:)用于控制事件:UIControlEventTouchUpInside];
redioBtn2.tag=++tagCount;
[cell.contentView addSubview:rediobn2];
UIButton*RediobN3=[[UIButton alloc]initWithFrame:CGRectMake(140,20,20,20)];
[redioBtn3 setImage:[UIImage IMAGENAME:@“radiounselect.png”]用于状态:UIControlStateNormal];
[redioBtn3添加目标:自我操作:@选择器(selectRadioButon:)用于控制事件:UIControlEventTouchUpInside];
redioBtn3.tag=++tagCount;
[cell.contentView addSubview:redioBtn3];
返回单元;
}
-(无效)选择RadioButon:(id)发件人{
btnTag=[发送者标签];
NSArray*arr=self.view.subview;
UITableView*tblCell=[arr objectAtIndex:0];
NSArray*ceraly=tblCell.subview;
对于(int i=0;i第一件事:您不应该每次加载单元格时都添加UIButton。如果(cell==nil)块
,您应该将它们添加到中
下一件事:您应该在单独的数组(NSMutableArray)中跟踪所选单选按钮索引(一个int值),并在if(cell==nil)块之外选择相应的UIButton
我希望能帮助你