如何从xcode中的自定义单元格中的多行中选择多个数据?

时间:2011-05-04 07:40:48

标签: iphone objective-c uitableview

代码: 检查一下:

#import <UIKit/UIKit.h>


@interface addsymptom : UITableViewController<UITableViewDelegate, UITableViewDataSource> {

    NSMutableArray *listData;

}
@property(nonatomic,retain)NSMutableArray *listData;
@end

data.m:
============
#import "CustomCell.h"

@implementation addsymptom


@synthesize listData;

- (void)viewDidLoad
{

    self.navigationItem.title= @"Symptoms";

    self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"mainbg.png"]];
    listData =[[NSMutableArray alloc] init];

    [listData addObject:@"Backpain"];
    [listData addObject:@"headache"];
    [listData addObject:@"vomitting"];
    [listData addObject:@"Bodypain"];
    [listData addObject:@"thursday"];
    [listData addObject:@"maleria"];
    [listData addObject:@"Food poisioning"];

    UIBarButtonItem *saveButton=[[UIBarButtonItem alloc]initWithTitle:@"save" 
                                                                style:UIBarButtonSystemItemDone
                                                               target:self 
                                                               action:@selector(save)];



    self.navigationItem.rightBarButtonItem = saveButton;

    [saveButton release];

    [super viewDidLoad];

}

-(void)save {
    [self.navigationController popViewControllerAnimated:YES];
}






- (UITableViewCell *)tableView:(UITableView *) tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCell";

    CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (CustomCell *) currentObject;
                break;
            }
        }
    }

    cell.textLabel.text = [listData  objectAtIndex:indexPath.row];

    return cell;
}


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

}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload
{
    self.listData = nil;
}


- (void)dealloc
{   

    [super dealloc];
}



@end

custom cell.h:
==========
#import <Foundation/Foundation.h>


@interface CustomCell :UITableViewCell {
    IBOutlet UILabel *textLabel;
    IBOutlet UIButton *bttn;
    BOOL isChecked;
    NSString *ans;


}
@property (nonatomic,retain) IBOutlet UILabel *textLabel;
@property (nonatomic,assign) BOOL isChecked;
@property(nonatomic,assign)IBOutlet UIButton *bttn;
-(void)checkBoxClicked:(id)sender;

@end

customcell.m:
==============
#import "CustomCell.h"


@implementation CustomCell
@synthesize textLabel,bttn,isChecked;
-(id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithFrame:frame ]) {

        textLabel = [[UILabel alloc]init];

        textLabel.textAlignment = UITextAlignmentLeft;

    }
    return self;
}

NSString *ABC=NULL;
NSString *xyz=NULL;
-(void)checkBoxClicked:(id)sender{

    UIButton *tappedButton = (UIButton*)sender;


    if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"NotSelected.png"]]) {
        [sender  setImage:[UIImage imageNamed: @"IsSelected.png"] forState:UIControlStateNormal];
        UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Message" 
                                                         message:textLabel.text
                                                        delegate:self
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil,nil]autorelease];
        [alert show];

        ans = textLabel.text;


        NSLog(@"%@",ans);


    }

    else {
        [sender setImage:[UIImage imageNamed:@"NotSelected.png"]forState:UIControlStateNormal];
    }



}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

}
- (void)dealloc {

    [super dealloc];
}

@end

3 个答案:

答案 0 :(得分:2)

检查这个例子它有一些类似你的实现

http://cocoawithlove.com/2009/01/multiple-row-selection-and-editing-in.html

祝你好运

答案 1 :(得分:0)

如果您希望Mail app style多个单元格选择,这post会有帮助吗?另一个post有很好的答案。

答案 2 :(得分:0)

//CustomCell.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>


@interface CustomCell :UITableViewCell {
    IBOutlet UILabel *textLabel;
    IBOutlet UIButton *bttn;
    BOOL isChecked;
    NSString *ans;


}
@property (nonatomic,retain) IBOutlet UILabel *textLabel;
@property (nonatomic,assign) BOOL isChecked;
@property(nonatomic,assign)IBOutlet UIButton *bttn;
//-(void)checkBoxClicked:(id)sender;

@end



//CustomCell.m


#import "CustomCell.h"


@implementation CustomCell


@synthesize textLabel,bttn,isChecked;



-(id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithFrame:frame ]) {

        textLabel = [[UILabel alloc]init];

        textLabel.textAlignment = UITextAlignmentLeft;

    }
    return self;
}

NSString *ABC=NULL;
NSString *xyz=NULL;
//-(void)checkBoxClicked:(id)sender{
//  
//    UIButton *tappedButton = (UIButton*)sender;
//  
//  
//    if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"chkbox_unckd.png"]]) {
//        [sender  setImage:[UIImage imageNamed: @"chkbox_ckd.png"] forState:UIControlStateNormal];
//        UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Message" 
//                                                         message:textLabel.text
//                                                        delegate:self
//                                               cancelButtonTitle:@"OK"
//                                               otherButtonTitles:nil,nil]autorelease];
//        [alert show];
//      
//        ans = textLabel.text;
//      
//      
//        NSLog(@"%@",ans);
//      
//      
//    }
//  
//    else {
//        [sender setImage:[UIImage imageNamed:@"chkbox_unckd.png"]forState:UIControlStateNormal];
//    }
//  
//  
//  
//}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

//  [bttn setImage:[UIImage imageNamed:@"chkbox_ckd.png"] forState:UIControlStateNormal];
    [super setSelected:selected animated:animated];

}
- (void)dealloc {

    [super dealloc];
}

@end



//addsymptom.h


#import <UIKit/UIKit.h>

@interface addsymptom : UIViewController<UITableViewDelegate, UITableViewDataSource> {

    NSMutableArray *listData;
    IBOutlet UITableView *table;

    NSMutableArray *TitlesArray;


    NSMutableArray *isCheckedArr;
}
@property(nonatomic,retain)NSMutableArray *listData;

- (void)saveTheChanges;


@end


//addsymptom.m

#import "addsymptom.h"
#import "CustomCell.h"



@implementation addsymptom


@synthesize listData;

- (void)viewDidLoad
{

    self.navigationItem.title= @"Symptoms";

    self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"mainbg.png"]];

    listData =[[NSMutableArray alloc] init];

    [listData addObject:@"Backpain"];
    [listData addObject:@"headache"];
    [listData addObject:@"vomitting"];
    [listData addObject:@"Bodypain"];
    [listData addObject:@"thursday"];
    [listData addObject:@"maleria"];
    [listData addObject:@"Food poisioning"];

    UIBarButtonItem *saveButton=[[UIBarButtonItem alloc]initWithTitle:@"save" style:UIBarButtonSystemItemDone target:self action:@selector(saveTheChanges)];

    isCheckedArr = [[NSMutableArray alloc] init];

    for (int i=0; i<[listData count]; i++) {
        [isCheckedArr addObject:@"0"];
    }

    self.navigationItem.rightBarButtonItem = saveButton;

    [saveButton release];

    TitlesArray = [[NSMutableArray alloc] init];    
}

-(void)save {
    [self.navigationController popViewControllerAnimated:YES];
}




// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [listData count];
}




- (UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCell";

    CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (CustomCell *) currentObject;
                break;
            }
        }
    }

    cell.textLabel.text = [listData  objectAtIndex:indexPath.row];

    [cell.bttn setTag:indexPath.row];

    [cell.bttn addTarget:self action:@selector(updateCheckbox:) forControlEvents:UIControlEventTouchUpInside];



    if ([[isCheckedArr objectAtIndex:indexPath.row] isEqualToString:@"1"])
    {
        [cell.bttn setImage:[UIImage imageNamed:@"chkbox_ckd.png"] forState:UIControlStateNormal];
    }
    else
    {
        [cell.bttn setImage:[UIImage imageNamed:@"chkbox_unckd.png"] forState:UIControlStateNormal];
    }



    return cell;
}




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

}


- (void)updateCheckbox:(id)sender {

    if ([[isCheckedArr objectAtIndex:[sender tag]] isEqualToString:@"0"]) {

        [isCheckedArr replaceObjectAtIndex:[sender tag] withObject:@"1"];

    }
    else if ([[isCheckedArr objectAtIndex:[sender tag]] isEqualToString:@"1"]) {
        [isCheckedArr replaceObjectAtIndex:[sender tag] withObject:@"0"];

        for (int i=0; i<[TitlesArray count]; i++) {
            if ([[listData objectAtIndex:[sender tag]] isEqualToString:[TitlesArray objectAtIndex:i]]) {
                [TitlesArray removeObjectAtIndex:i];
        }
        }   
    }

    [table reloadData];

}




- (void)saveTheChanges {

    for (int i=0; i<[isCheckedArr count]; i++) {
        if ([[isCheckedArr objectAtIndex:i] isEqualToString:@"1"]) {
            for (int j=0; j<[TitlesArray count]; j++) {
                if ([[listData objectAtIndex:i] isEqualToString:[TitlesArray objectAtIndex:j]]) {
                    [TitlesArray removeObjectAtIndex:j];
                }

            }
            [TitlesArray addObject:[listData objectAtIndex:i]];


        }

    }

    for (int i=0; i<[TitlesArray count]; i++) {
        NSLog(@"%@",[TitlesArray objectAtIndex:i]);

    }

    NSLog(@"=======");
}

- (void)viewDidUnload
{
//    self.listData = nil;
}


- (void)dealloc
{   

    [listData release];

    [TitlesArray release];
    [isCheckedArr release];

    [super dealloc];
}

@end