tableview Cell中的单选按钮

时间:2016-01-03 06:37:13

标签: ios objective-c iphone uitableview

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

我还在Tabelview.xib left side添加了一个按钮。该按钮将充当radiobutton来检查和取消选中。现在当我运行我的所有表视图时都显示取消选中图像按钮。我该如何选中或取消选中?

这是我的代码:

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

只有一个单选按钮应该检查,并且取消选中其他行radiobutton。

我使用了两张图片:

  1. selectBtn.png
  2. deselectBtn.png
  3. 我错过了什么?我尝试在我的项目中添加这些步骤,但我是初学者,因此无法完美地完成并且更多的崩溃。

    @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;
            }
        }
    }
    

    Here is my project

2 个答案:

答案 0 :(得分:1)

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

我已为您完成此操作我建议您使用逻辑并且不要发布您在此处遇到的每个问题。

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

答案 1 :(得分:0)

首先连接 IBOutlet checkBtn 按钮

Meteor.call('sendEmail',
  'mail@gmail.com',
  'postmaster@mailgun.org',
  'some text' + thisGame.gameNumber,
  '<div> some HTML body' + some.prop + '</div>'
);