iPhone-Grouped Table View为每行添加两个单选按钮

时间:2011-08-10 05:45:17

标签: iphone

我想在每一行显示两个单选按钮,我需要为两个按钮设置操作我在设置操作时遇到问题,并在每行的正确位置显示文本和单选按钮,如果有人请帮忙。< / p>

这是我的代码

提前致谢

.h文件

#import <UIKit/UIKit.h>
//@class CustomCellQuestionnaireList;

@interface QuestionnaireListView : UIViewController <UITableViewDataSource, UITableViewDelegate,UINavigationControllerDelegate,UISearchBarDelegate> 
{
  //NSMutableArray *dataArray;
  UITableView       *theTableView;
  UILabel           *lbl_child;
  UILabel           *lbl_title;
  UILabel           *lbl_time;
  NSMutableArray    *tableData;//will be storing data that will be displayed in table
  NSMutableArray    *tableSubData;
  //NSMutableArray  *tableTitleArray;
  //CustomCellQuestionnaireList *cell;
    IBOutlet UIButton *myButton;
    IBOutlet UIButton *myButton2;
    BOOL isSelected;
    IBOutlet UIView *view1;
    IBOutlet UIBarButtonItem *saveBtn;
    IBOutlet UIBarButtonItem *discardBtn;
    IBOutlet UIBarButtonItem *okBtn;
    //IBOutlet UIToolbar *m_toolBar;
    //IBOutlet UIToolbar *m_okToolBar;
}

@property (nonatomic,retain) IBOutlet UILabel *lbl_child;
@property (nonatomic,retain) IBOutlet UILabel *lbl_title;
@property (nonatomic,retain) IBOutlet UILabel *lbl_time;
@property (nonatomic,retain) IBOutlet UITableView * theTableView;
//@property(nonatomic,retain) NSMutableArray *dataArray;
@property(nonatomic, retain) NSMutableArray *tableData;
@property(nonatomic, retain)NSMutableArray *tableSubData;
@property(nonatomic, retain)UIView *view1;
@property(nonatomic, retain)UIBarButtonItem *saveBtn;
@property(nonatomic, retain)UIBarButtonItem *discardBtn;
@property(nonatomic, retain)UIBarButtonItem *okBtn;
//@property(nonatomic, retain)UIToolbar *m_toolBar;
//@property(nonatomic, retain)UIToolbar *m_okToolBar;

- (IBAction)onClickLeftArrow;
- (IBAction)onClickRightArrow;
//- (IBAction)savePressed:(id)sender;
- (IBAction)selectRadioButon:(UIButton *)button;
//-(IBAction)okPressed:(id)sender;

@end

.m文件

@implementation QuestionnaireListView

@synthesize tableData, tableSubData;
@synthesize theTableView;
//@synthesize dataArray;
@synthesize lbl_child;
@synthesize lbl_title;
@synthesize lbl_time;
@synthesize view1;
@synthesize saveBtn;
@synthesize discardBtn;
@synthesize okBtn;
//@synthesize m_toolBar;
//@synthesize m_okToolBar;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
        UIButton *button1 =  [UIButton buttonWithType:UIButtonTypeCustom];
        [button1 setImage:[UIImage imageNamed:@"saveBtn.png"] forState:UIControlStateNormal];
        [button1 setFrame:CGRectMake(0, 0, 66, 34)];
        [button1 addTarget:self action:@selector(saveBtn:) forControlEvents:UIControlEventTouchUpInside];
        [button1 setTitle:@"Save" forState:UIControlStateNormal];

        UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
        NSMutableArray *toolBarItems = [[[NSMutableArray alloc] init] autorelease];
        [toolBarItems addObject:barButtonItem1];
        //[self setToolbarItems:toolBarItems];

        UIButton *button2 =  [UIButton buttonWithType:UIButtonTypeCustom];
        [button2 setImage:[UIImage imageNamed:@"okBtn.png"] forState:UIControlStateNormal];
        [button2 setFrame:CGRectMake(100, 0, 66, 34)];
        //[button2 addTarget:self action:@selector(home:) forControlEvents:UIControlEventTouchUpInside];
        //[button2 setTitle:@"Home" forState:UIControlStateNormal];



        UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
        [toolBarItems addObject:barButtonItem2];
        button2.hidden = YES;
        //[self setToolbarItems:toolBarItems];

        UIButton *button3 =  [UIButton buttonWithType:UIButtonTypeCustom];
        [button3 setImage:[UIImage imageNamed:@"okBtn.png"] forState:UIControlStateNormal];
        [button3 setFrame:CGRectMake(150, 0, 66, 34)];
        //[button4 addTarget:self action:@selector(okBtn:) forControlEvents:UIControlEventTouchUpInside];
        //[button4 setTitle:@"Home" forState:UIControlStateNormal];

        UIBarButtonItem *barButtonItem3 = [[UIBarButtonItem alloc] initWithCustomView:button3];
        [toolBarItems addObject:barButtonItem3];
        button3.hidden = YES;

        UIButton *button4 =  [UIButton buttonWithType:UIButtonTypeCustom];
        [button4 setImage:[UIImage imageNamed:@"discardBtn.png"] forState:UIControlStateNormal];
        [button4 setFrame:CGRectMake(200, 0, 66, 34)];
        [button4 addTarget:self action:@selector(discardBtn:) forControlEvents:UIControlEventTouchUpInside];
        [button4 setTitle:@"Discard" forState:UIControlStateNormal];

        UIBarButtonItem *barButtonItem4 = [[UIBarButtonItem alloc] initWithCustomView:button4];
        [toolBarItems addObject:barButtonItem4];
        [self setToolbarItems:toolBarItems];


    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //[self.theTableView.layer setCornerRadius:30.0];
    // create button
    //UIButton* logOutButton = [UIButton buttonWithType:101]; // left-pointing shape!
    UIButton *logOutButton = [UIButton buttonWithType:100];
    [logOutButton setImage:[UIImage imageNamed:@"exitBtn.png"] forState:UIControlStateNormal];
    [logOutButton addTarget:self action:@selector(logoutButtonTouched) forControlEvents:UIControlEventTouchUpInside];
    [logOutButton setTitle:@"Logout" forState:UIControlStateNormal];
    UIBarButtonItem* logOutItem = [[UIBarButtonItem alloc] initWithCustomView:logOutButton];
    self.navigationItem.leftBarButtonItem = logOutItem;


    [lbl_title setText:@"Store View"];
    [lbl_title setTextColor:[UIColor whiteColor]];
    //NSString *date = [[NSDate date] dateFormat];
    NSString *date = [NSDate date];
    NSLog(@"date:%@",date);
    [lbl_time setText:[NSString stringWithFormat:@"%@",date]];
    [lbl_time setTextColor:[UIColor whiteColor]];
    // add to toolbar, or to a navbar (you should only have one of these!)
    //[toolbar setItems:[NSArray arrayWithObject:backItem]];

    //tableTitleArray = [[NSMutableArray alloc]init];
//  [tableTitleArray addObject:@"General"];
//  [tableTitleArray addObject:@"Product"];

    tableData = [[NSMutableArray alloc] init];
    [tableData addObject:@"Is the floor clean?"];
    [tableData addObject:@"Are the food items stacked in freezer?"];

    tableSubData = [[NSMutableArray alloc] init];
    [tableData addObject:@"Has the snow been cleared at the entrance?"];
    [tableData addObject:@"Are milk products available"];


    view1.hidden = YES;
    //m_okToolBar.hidden= YES;

    //m_toolBar.barStyle = UIBarStyleBlackTranslucent;
    //m_toolBar.tintColor = [UIColor blackColor]; m_toolBar.alpha = 0.7;
    //UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nextBtn.png" style:UIBarButtonItemStyleBordered target:self action:@selector(customButtonHandler:)]];
    // btn.frame = CGRectMake(5, 20, 30, 30);
    //[btn release];
}


-(void)saveBtn:(id)sender {
    NSLog(@"Button pressed");
    view1.hidden = NO;
    UIButton *button1 =  [UIButton buttonWithType:UIButtonTypeCustom];
    [button1 setImage:[UIImage imageNamed:@"saveBtn.png"] forState:UIControlStateNormal];
    [button1 setFrame:CGRectMake(0, 0, 105, 34)];
    //[button1 addTarget:self action:@selector(saveBtn:) forControlEvents:UIControlEventTouchUpInside];
    //[button1 setTitle:@"Save" forState:UIControlStateNormal];

    UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
    NSMutableArray *toolBarItems = [[[NSMutableArray alloc] init] autorelease];
    [toolBarItems addObject:barButtonItem1];

    UIButton *button2 =  [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *button3 =  [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *button4 =  [UIButton buttonWithType:UIButtonTypeCustom];
    [button2 setImage:[UIImage imageNamed:@"okBtn.png"] forState:UIControlStateNormal];
    [button2 setFrame:CGRectMake(0, 0, 66, 34)];
    [button2 addTarget:self action:@selector(okBtn:) forControlEvents:UIControlEventTouchUpInside];
    [button2 setTitle:@"OK" forState:UIControlStateNormal];

    UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
    [toolBarItems addObject:barButtonItem2];
    [self setToolbarItems:toolBarItems];



    button1.hidden = YES;
    button2.hidden = NO;
    button3.hidden = YES;
    button4.hidden = YES;

}

-(void)okBtn:(id)sender {
    view1.hidden = YES;

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:3] animated:YES];

}


//-(void)viewWillAppear:(BOOL)animated
//{
//
//  //VizueraQAppDelegate *appDelegate = (VizueraQAppDelegate *)[[UIApplication sharedApplication] delegate];
//  //[appDelegate RefreshIndexArray];
//  
//}
- (void)logoutButtonTouched {   
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}

- (IBAction)onClickLeftArrow {  

    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)onClickRightArrow { 

    //[self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // release the array
//  self.dataArray = nil;
//  tableData = nil;
}

- (void)dealloc
{   
    //[cell release];
    [tableData release];
    [tableSubData release];
    [super dealloc];
}


#pragma mark - UIViewController delegate methods

- (void)didReceiveMemoryWarning
{
    // invoke super's implementation to do the Right Thing, but also release the input controller since we can do that  
    // In practice this is unlikely to be used in this application, and it would be of little benefit,
    // but the principle is the important thing.
    //
    [super didReceiveMemoryWarning];
}


//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
//{
//  return YES;
//}


#pragma mark - UITableView delegate methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableData!=nil && tableSubData!=nil) {
        return 2;
    }
    else {
        return 1;
    }
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return [tableData count];
    }
    else {
        return [tableSubData count];
    }

}

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


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


}

//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    //return ([NSString stringWithFormat:@"%@", [tableTitleArray objectAtIndex:section]]);

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        NSString *returnString = @"";
        if (tableData!=nil && section == 0) {
            returnString =  @"General";
        }
        else if(section == 1){
            returnString = @"Product";
        }
        return returnString;
    }


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

    static NSString *kCustomCellID = @"MyCellID";

    UITableViewCell *cell = (UITableViewCell *)[theTableView dequeueReusableCellWithIdentifier:kCustomCellID];
    //CustomCell *cell = (CustomCell *)[theTableView dequeueReusableCellWithIdentifier:kCustomCellID];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                       reuseIdentifier:kCustomCellID] autorelease];
        //cell = (CustomCell *)[[[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCustomCellID] autorelease];
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.tag=[indexPath row];
        NSInteger tagCount;
        myButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 48, 20, 20)]; 
        [myButton setImage:[UIImage imageNamed:@"radioBtn.png"] forState:UIControlStateNormal];
        [myButton addTarget:self action:@selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
        myButton.tag = ++tagCount;
        [cell.contentView addSubview:myButton];
        tagCount++;

        myButton2 = [[UIButton alloc]initWithFrame:CGRectMake(100, 48, 20, 20)]; 
        [myButton2 setImage:[UIImage imageNamed:@"radioBtn.png"] forState:UIControlStateNormal];
        [myButton2 addTarget:self action:@selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
        myButton2.tag = tagCount;
        [cell.contentView addSubview:myButton2];
        tagCount++;

        /*UILabel *m_label = [[UILabel alloc] init];
        m_label.text = @"Yes";
        m_label.textColor = [UIColor redColor];
        m_label.frame = CGRectMake(15, 48, 40, 40);
        [m_label release]; */

        CGRect labelRect = CGRectMake(35, 49, 30, 15);
        UILabel *label = [[UILabel alloc] initWithFrame:labelRect];
        //label.backgroundColor = [UIColor redColor];
        label.text = @"Yes";
        label.textAlignment = UITextAlignmentLeft;
        label.font = [UIFont boldSystemFontOfSize:14];
        label.tag = 10;
        [cell.contentView addSubview:label];
        [label release];

        CGRect labelRect1 = CGRectMake(125, 50, 30, 15);
        UILabel *label1 = [[UILabel alloc] initWithFrame:labelRect1];
        //label.backgroundColor = [UIColor redColor];
        label1.text = @"No";
        label1.textAlignment = UITextAlignmentLeft;
        label1.font = [UIFont boldSystemFontOfSize:14];
        label1.tag = 10;
        [cell.contentView addSubview:label1];
        [label1 release];
    }

    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if([tableData count]>indexPath.row)
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    cell.textLabel.frame = CGRectMake(0, 0, 20, 20);


    return cell;
}

- (IBAction)selectRadioButon:(UIButton *)button{

    for (UIButton *but in [self.view subviews]) {
        if ([but isKindOfClass:[UIButton class]] && ![but isEqual:button]) {
            [but setSelected:NO];
        }
    }
    if (!button.selected) {
        button.selected = !button.selected;
    }
    if (isSelected) {
            [button setImage:[UIImage imageNamed:@"radioBtn.png"] forState:UIControlStateNormal];
            isSelected = NO;
    }
    else {
        [button setImage:[UIImage imageNamed:@"radioBtn_active.png"] forState:UIControlStateNormal];
        isSelected = YES;
    }
}

1 个答案:

答案 0 :(得分:0)

将“initWithFrame”代码保存在if(cell == nil){}内,以便在绘制表格单元格时将所有“设置”UI元素移到if之外。

请参阅我的代码示例:

Can't Change Transparent BG in Custom UITableViewCell (Grouped Table View)