tableview 单元格中的单选按钮 [英] Radio button in tableview Cell

查看:38
本文介绍了tableview 单元格中的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我使用 UITableviewcell 的子类作为 Tableview.我正在创建一些虚拟的 json 值,并将传递给我的 tableview 单元格中的标签.

Here I am using subclass of UITableviewcell as Tableview. And I am creating some dummy json value and I am passing to the label in my tableview cell.

我还在左侧Tabelview.xib中添加了一个按钮.该按钮将作为单选按钮来检查和取消选中.现在,当我运行我的所有表视图时,都显示取消选中图像按钮.如何选中或取消选中?

And also i added one button in my Tabelview.xib at left side. That button will act as radiobutton to check and uncheck. Now when I run my all table view are showing uncheck image button. How can I check or uncheck?

这是我的代码:

Tableview.h

#import <UIKit/UIKit.h>

@interface TableView : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *lab1;

@property (weak, nonatomic) IBOutlet UILabel *lab2;


@property (weak, nonatomic) IBOutlet UILabel *lab3;
@property (weak, nonatomic) IBOutlet UIButton *checkBtn;


@end 

Tableview.m

#import "TableView.h"

@implementation TableView
@synthesize lab1 = _lab1;
@synthesize lab2 = _lab2;
@synthesize lab3 = _lab3;
@synthesize checkBtn;



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

Viewcontroller.m

#import "ViewController.h"
#import "TableView.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation ViewController
{


    NSArray *jsonObject;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //NSArray *jsonObject;
    jsonObject = @[
                   @{
                       @"partner": @"50",
                       @"gamer": @"199",
                       @"pointer": @"144"

                   },
                   @{
                       @"partner": @"80",
                       @"gamer": @"112",
                       @"pointer": @"11"                   },
                   @{
                       @"partner": @"30",
                       @"gamer": @"112",
                       @"pointer": @"14"


                   },
                   @{
                       @"partner": @"50",
                       @"gamer": @"100",
                       @"pointer": @"199"
                   },
                  @{
                       @"partner": @"50",
                       @"gamer": @"19",
                       @"pointer": @"44"

                   },
                   @{
                       @"partner": @"2000",
                       @"gamer": @"500",
                       @"pointer": @"1000"
                   }
                   ];

    NSError *err;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonObject options:NSJSONWritingPrettyPrinted error:nil];



    NSString * jsonString=[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    NSLog(@"%@",jsonString);




}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [jsonObject count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TableView";
    TableView *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.lab1.text = [jsonObject objectAtIndex:indexPath.row][@"gamer"];
    cell.lab2.text = [jsonObject objectAtIndex:indexPath.row][@"partner"];
    cell.lab3.text = [jsonObject objectAtIndex:indexPath.row][@"pointer"];


    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 78;
}

只有一个单选按钮应选中,其他行单选按钮应取消选中.

At only one radio button should check and other rows radiobutton are should uncheck.

我使用了两张图片:

  1. selectBtn.png
  2. 取消选择Btn.png

我错过了什么?我尝试将这些步骤添加到我的项目中,但我是一个初学者,因此无法完美完成并且出现更多崩溃.

What am I missing? I tried these steps to added in my projects but I am a beginner so not able to do perfectly and getting more crash.

@interface RootViewController : UITableViewController {
    NSMutableArray *radioButtonArray;
}

@property (nonatomic ,retain)NSMutableArray *radioButtonArray;
in tableviewController.h.m

- (void)viewDidAppear:(BOOL)animated {
    radioButtonArray = [NSMutableArray new];
    for (int i = 0; i < 30; i ++) {
        UIButton *radioButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [radioButton setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
        [radioButton setImage:[UIImage imageNamed:@"radio-on.png"] forState:UIControlStateSelected];
        [radioButton setFrame:CGRectMake(0, 0, 44, 44)];
        [radioButton addTarget:self action:@selector(radioButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [radioButtonArray addObject:radioButton];
    }
    [super viewDidAppear:animated];
}
and give it a (IBAction) void

- (IBAction)radioButtonPressed:(UIButton *)button{
    [button setSelected:YES];
    // Unselect all others.
    for (UIButton *other in radioButtonArray) {
        if (other != button) {
            other.selected=NO;
        }
    }
}

这是我的项目.

推荐答案

请使用数组保存所有单元格.并在 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 委托以获取选定的行并使其他行按钮并选择选定的行按钮.

Please use an array to save all the cells. and on -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath delegate to get which row is selected and make the others rows button and select the selected row button.

我已经为你做了这件事,我建议你使用逻辑,不要把你面临的每个问题都张贴在这里.

I have done this for you i am suggesting you to use the logics and don't post every issue you are facing here.

https://www.dropbox.com/s/j02yotvevxfp821/jsonTableView%203.zip?dl=0

这篇关于tableview 单元格中的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆