2015-03-13 28 views
0

我是ios開發新手。我是在Storyboard中創建應用程序,最初是UItableviewController,帶有一個原型單元格,帶有按鈕和標籤。所述按鈕具有IBAction爲在它的UITableViewCell類的方法,所述類具有代表的UITableViewController,委託方法不被調用。但是剖面視圖中的圖像是變化的。我在這裏發佈我的完整代碼。UITableView節頭可點擊委託不請致電

Download whole project from Link

ContactHeaderViewCell.h

​​

ContactHeaderViewCell.m

@implementation ContactHeaderViewCell 

@synthesize HeaderDelegate,section; 
- (void) setDelegate:(id)delegate 
{ 
self.HeaderDelegate = delegate; 
} 
- (void)awakeFromNib { 
// Initialization code 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 
[super setSelected:selected animated:animated]; 

// Configure the view for the selected state 
} 

- (IBAction)btnCheckBox_click:(id)sender { 
self.btnCheckBox.selected = !self.btnCheckBox.selected; 

if (self.btnCheckBox.selected) 
{ 
    if ([self.HeaderDelegate respondsToSelector:@selector(UnTickCheckbox:)]) 
    { 
     [self.HeaderDelegate UnTickCheckbox:self.section]; 
    } 
} else 
{ 
    if ([self.HeaderDelegate respondsToSelector:@selector(TickCheckbox:)]) 
    { 
     [self.HeaderDelegate TickCheckbox:self.section]; 
    } 
} 
} 

ContactTableViewController.m

#import "ContactDetail.h" 
#import "ContactHeaderViewCell.h" 

@interface ContactTableViewController : UITableViewController<SectionClick> 
@property(nonatomic) ContactDetail *contacts; 
@property(nonatomic) NSMutableArray *ContactList; 
@end 

ContactTableViewController.m

#import "ContactTableViewController.h" 
#import <AddressBook/AddressBook.h> 
#import "ContactHeaderViewCell.h" 
#import "UserDetailViewCell.h" 

@interface ContactTableViewController() 

@end 

@implementation ContactTableViewController 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
    } 
    return self; 
} 
- (void)viewDidLoad { 
    [self ArrayContactFunc]; 
    [super viewDidLoad]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [self.ContactList count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    self.contacts =(self.ContactList)[section]; 
    return (self.contacts.Isopen) ? [self.contacts.mobileNumbers count] : 0; 
} 

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    static NSString *HeaderIdentifier = @"HeaderCell"; 
    self.contacts = (self.ContactList)[section]; 
    ContactHeaderViewCell *HeaderView = (ContactHeaderViewCell *)[self.tableView dequeueReusableCellWithIdentifier:HeaderIdentifier]; 
    if (HeaderView == nil){ 
     [NSException raise:@"headerView == nil.." format:@"No cells with matching CellIdentifier loaded from your storyboard"]; 
      } 
    HeaderView.lblName.text = self.contacts.fullName; 
     if(self.contacts.IsChecked) 
     { 
      [HeaderView.btnCheckBox setImage:[UIImage imageNamed:@"Unchecked.png"] forState:UIControlStateSelected]; 
     } 
     else 
     { 
      [HeaderView.btnCheckBox setImage:[UIImage imageNamed:@"Checked.png"] forState:UIControlStateSelected]; 
     } 
    return HeaderView; 
} 

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    self.contacts = (self.ContactList)[indexPath.section]; 
    static NSString *DetailCellIdentifier = @"DetailCell"; 
    UserDetailViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:DetailCellIdentifier]; 
    if(!cell) 
    { 
     [NSException raise:@"headerView == nil.." format:@"No cells with matching CellIdentifier loaded from your storyboard"]; 
    } 

    cell.lblDetail.text = (self.contacts.mobileNumbers)[indexPath.row]; 
    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 60.0; 
} 
-(void)UnTickCheckbox:(NSInteger)section 
{ 
// self.contacts = (self.ContactList)[section]; 
// self.contacts.IsChecked = NO; 
// [self.tableView reloadData]; 
} 

-(void)TickCheckbox:(NSInteger)section 
{ 
// self.contacts = (self.ContactList)[section]; 
// self.contacts.IsChecked = YES; 
// [self.tableView reloadData]; 
} 

預先感謝。

回答

0

您必須添加此行 [HeaderView setHeaderDelegate:self]; (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

+0

這就是工作。謝謝。但是SectionHeader圖像不會改變。爲什麼? – 2015-03-13 15:41:25