gpt4 book ai didi

iphone - TableView Custom Cell中添加Radio Button外部无法访问

转载 作者:搜寻专家 更新时间:2023-10-30 19:49:36 25 4
gpt4 key购买 nike

我在 UITableView 的自定义单元格中添加单选按钮,并在其上调用一个方法来更改其图像或背景图像。我可以设置来自发件人的选定按钮图像,但我想将其他按钮设置到我无法设置的原始位置,因为下面的功能需要看起来像单元格中正确的单选按钮

我在使用此方法获取正确的索引路径时也遇到了问题 - (void) TableRadioButtonSelection:(id)sender;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *RadioIdentifier = @"RadioCellIdentifier";

RadioBtnCell *myRadiocell = (RadioBtnCell *)[tableView dequeueReusableCellWithIdentifier:RadioIdentifier];
if(myRadiocell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"RadioBtnCell" owner:self options:nil];
myRadiocell = radioCell;

//[myRadiocell.radioBtn1 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
myRadiocell.radioBtn1.tag = ButtonTag;
//[myRadiocell.radioBtn2 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
myRadiocell.radioBtn2.tag = ButtonTag1;
//[myRadiocell.radioBtn3 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
myRadiocell.radioBtn3.tag = ButtonTag2;
myRadiocell.tag = RadioTag;

[myRadiocell.radioBtn1 addTarget:self action:@selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside];
[myRadiocell.radioBtn2 addTarget:self action:@selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside];
[myRadiocell.radioBtn3 addTarget:self action:@selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside];

myRadiocell.backgroundView.backgroundColor = [UIColor clearColor];
self.radioCell = nil;

}

return myRadiocell;

}

- (void) TableRadioButtonSelection:(id)sender {

//RadioBtnCell *buttonCell = (RadioBtnCell*)[[btn superview] superview];

RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView viewWithTag:RadioTag];
UIButton *mybtn = (UIButton *)[cell.contentView viewWithTag:ButtonTag];
UIButton *mybtn1 = (UIButton *)[cell.contentView viewWithTag:ButtonTag1];
UIButton *mybtn2 = (UIButton *)[cell.contentView viewWithTag:ButtonTag2];

[mybtn setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
[mybtn1 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
[mybtn2 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];

//NSLog(@"%@",[cell subviews]);
UIView* mysubview = [cell.contentView viewWithTag:ButtonTag]; // Make sure your UITextField really *is* the last object you added.
UIView* mysubview1 = [cell.contentView viewWithTag:ButtonTag1];
UIView* mysubview2 = [cell.contentView viewWithTag:ButtonTag2];

// I am unable to set Image of other buttons apart from sender.

//for(UIView *item in [mysubview subviews]) {

if ([mysubview isKindOfClass:[UIButton class]]) {
UIButton *newBtn = (UIButton*)mysubview;
UIButton *newBtn1 = (UIButton*)mysubview1;
UIButton *newBtn2 = (UIButton*)mysubview2;

NSLog(@"OK %@",newBtn);
//[newBtn setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
[newBtn setImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
[newBtn1 setImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
[newBtn2 setImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];

}
//}

NSIndexPath *indexPath = [self.questionTblView indexPathForCell:cell];
NSLog(@"%d",indexPath.row);

// Below code is setting Image of selected Button properly as checked but above code for making button onto original state is not setting button image or background image

UIButton *btn = (UIButton *)sender;
[btn setImage:[UIImage imageNamed:@"radiobtn_chk.png"] forState:UIControlStateNormal];
//[btn setBackgroundImage:[UIImage imageNamed:@"radiobtn_chk.png"] forState:UIControlStateNormal];
}

最佳答案

试着把这一行改成这样;-

RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView viewWithTag:RadioTag];

to

RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView cellForRowAtIndexPath:*indexPathofthecell*];

通过这种方式,您可以访问表格 View 单元格。之后,您可以从单元格内容 View 中获取按钮并进行进一步编码。

假设如果您不知道 indexPath 那么您可以从 TableView 的 rowNumber 创建 indexPath(我认为您已经将表行号存储在 RadioTag 中,如果是,那么您可以将此字段用作参数)。

关于iphone - TableView Custom Cell中添加Radio Button外部无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12249337/

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