gpt4 book ai didi

iphone - 当单选按钮选择检查

转载 作者:行者123 更新时间:2023-11-28 17:44:51 24 4
gpt4 key购买 nike

我在 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;
}
}

}


}

最佳答案

第一件事:您不应该在每次加载单元格时都添加 UIButton。您应该将它们添加到 if (cell == nil) block 中。

下一步:您应该在单独的数组(NSMutableArray)中跟踪选定的单选按钮索引(int 值) ,并在 if (cell == nil) block 之外选择相应的 UIButton。

希望 this tutorial 会帮助你。

关于iphone - 当单选按钮选择检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6546846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com