這裏我使用UITableviewcell
的子類作爲Tableview
。我正在創建一些虛擬json值,並將其傳遞給我的tableview單元格中的標籤。tableview中的單選按鈕單元格
而且我還在Tabelview.xib
的left side
處添加了一個按鈕。該按鈕將作爲單選按鈕來檢查和取消選中。現在,當我運行我的所有表格視圖都顯示取消選中圖像按鈕。我如何檢查或取消選中?
這裏是我的代碼:
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;
}
只有一個單選按鈕應該檢查,其他行單選按鈕應該取消選中。
我用了兩個圖像:
- selectBtn.png
- deselectBtn.png
我缺少什麼?我嘗試了這些步驟來添加到我的項目中,但我是初學者,所以無法完美地完成並且導致更多的崩潰。
@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;
}
}
}
但滾動回來,我看不到我已經選擇的單選按鈕 – david
還有一個想法。我需要選擇單選按鈕。不是表格視圖單元格.......當我向下滾動時,我選擇的按鈕會自動取消選擇 – david
下載新代碼我已更新鏈接。要不選擇行,您可以在選擇部分中設置單元格的屬性none。 –