2016-01-04 128 views
1

我在Tbalview.xib中有一個tableviewUITableview的子類。我添加了三個標籤,左邊是一個checkBtn。這裏是我的代碼:單選按鈕不選擇和取消選擇

Tableview.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; 

Tableview.m

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

Viewcontroller.m

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

@interface ViewController()<UITableViewDataSource,UITableViewDelegate> 

@end 

@implementation ViewController 
{ 

    NSMutableArray *cells_Array; 
    NSArray *jsonObject; 

    int selectedIndex; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    cells_Array = [[NSMutableArray alloc] init]; 

    selectedIndex = -1; 

    // 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 
{ 
    [cells_Array removeAllObjects]; 

    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"]; 

    if(indexPath.row == selectedIndex) 
    { 
     [cell.checkBtn setSelected:true]; 
    } 

    [cells_Array addObject:cell]; 

    return cell; 
} 

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

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    for (TableView *view in cells_Array) { 
     [view.checkBtn setSelected:false]; 
    } 
    TableView *cellView = [self.containerTableView cellForRowAtIndexPath:indexPath]; 

    selectedIndex = (int)indexPath.row; 

    [cellView.checkBtn setSelected:true]; 
} 

@end 

我有兩個圖像作爲selectBtn.png(選擇)和DeselectBtn .png(取消選中)。

我從SO得到了這段代碼。我是iOS的初學者,因此無法找到我的錯誤在哪裏,而且在我的代碼中,他們提供了一些解決方案,但沒有調用這兩個工作.png images

這是我從SO那裏得到的幫助。只有這個相同的代碼。然而,這是行不通的,當我自己嘗試。這裏是代碼Project

這上面的項目是很好的工作。同樣喜歡我試過,但並非用於radiobutton.Here工作以及我自己的項目鏈接own projetc that not wroking

+0

檢查出口checkBtn是否與TableView.xib中的按鈕綁定 – bunty

+0

@bunty是的,只有所有正確的。請參閱我的更新後的帖子我已經添加了我的原始項目供您參考 – david

+0

在附加的項目中,您可以一次選擇一個正常工作的選項。你想多選? – bunty

回答

1

enter image description here

在TableView.xib,按鈕的選擇的圖像沒有被設置。選擇按鈕的選定狀態併爲圖像選擇selectBtn.png。

在按鈕的屬性窗口中,點擊狀態配置下拉菜單,選擇選擇狀態,然後在陰影顏色下面,有圖像字段;寫入快照中給出的selectBtn.png。這將指定當按鈕被選中時圖像是什麼。

+0

在.xib中,我選擇按鈕作爲Deselect.png ...這是你說什麼或任何其他..這是我在ios初學者。請解釋我要改變什麼 – david

+0

當我的應用程序運行我的按鈕時應該在'deselectBtn.png'中,所以當我點擊我的單元格時,我的按鈕應該改爲'selectBtn.png' – david

+0

在屬性窗口中,點擊狀態配置下拉菜單,選擇選中狀態,然後在陰影顏色下方顯示圖像字段;寫入快照中給出的selectBtn.png。這將指定當按鈕被選中時圖像是什麼。 – bunty