2013-06-03 90 views
0

我有三個按鈕和一些其他組件的自定義tableviewcell。自定義tableview單元格內的彈出窗口

enter image description here 我已經在彈出窗口中添加了datepicker(我已經使用FPPopover庫來執行此操作)以獲取「開始日期」的值。在這裏,我迄今所做的,

「的cellForRowAtIndexPath」方法

MainTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MainTableViewCell"]; 

if (!cell) { 
    cell = [[MainTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:@"MainTableViewCell"]; 

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

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

} 
... 

... 

[cell.startDateMainBtn addTarget: self action: @selector(startDateButtonPressed:) forControlEvents: UIControlEventTouchUpInside]; 
cell.startDateMainBtn.tag = indexPath.row; 
cell.startDateMainBtn.titleLabel.tag = 1; 

,並在「startDateButtonPressed」方法我已經做了以下。

- (void) startDateButtonPressed:(id) sender 
{ 
NSUInteger senderId = [sender tag]; 

if (!self.popoverContent) { 
    self.popoverContent = [[UIViewController alloc] init]; //ViewController 
} 
UIView *popoverView = [[UIView alloc] init]; //view 
popoverView.backgroundColor = [UIColor blackColor]; 

datePicker = [[UIDatePicker alloc] init]; //Date picker 
datePicker.frame = CGRectMake(0, 0, 320, 216); 
datePicker.datePickerMode = UIDatePickerModeDate; 
[datePicker setTag:senderId]; 
[datePicker addTarget:self action:@selector(selectedDate:) forControlEvents:UIControlEventValueChanged]; 
[popoverView addSubview:datePicker]; 

self.popoverContent.view = popoverView; 
popover.alpha = 0.8; 
popover = [[FPPopoverController alloc] initWithViewController:self.popoverContent]; 
popover.contentSize = CGSizeMake(340, 266); 
popover.arrowDirection = FPPopoverArrowDirectionUp; 
[popover presentPopoverFromView:sender]; 

switch ([[sender titleLabel] tag]) { 

     //START DATE 
    case 1: 
    { 
     self.buttonTypeTag = 1; 
    } 
     break; 

    default: 
     break; 
} 

}

而在「selectedDate」方法,

-(void)selectedDate:(id) sender 
{ 
NSLog(@"SELECTED Date %@ , Row %i",datePicker.date, datePicker.tag); 

NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
[df setDateFormat:@"yyyy,MM,dd"]; 

// UIButton *btn = (UIButton *)sender; 
// [btn setTitle:@"date" forState:UIControlStateNormal]; 

switch (self.buttonTypeTag) { 

     //START DATE 
    case 1: 
    {   
//   [self.cell.startDateMainBtn setTitle:[df stringFromDate:datePicker.date] forState:UIControlStateNormal]; 
     NSString *dateStr = [df stringFromDate:datePicker.date]; 
     NSLog(@"SELECTED Date %@",dateStr); 
    } 
     break; 

    default: 
     break; 
} 

[popover dismissPopoverAnimated:YES]; 
} 

所有工作正常!但我的問題是我無法將選定日期字符串(通過彈出窗口)值分配給開始日期按鈕標題。我怎樣才能做到這一點?有沒有簡單的方法來做到這一點?

回答

2

試試這個代碼:

在.h文件中

UIButton *btn; 

在.m文件

-(void)startDateButtonPressed:(id) sender 
{ 

btn=(UIButton *)sender; 
} 
-(void)selectedDate:(id) sender 
{ 
NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
[df setDateFormat:@"yyyy,MM,dd"]; 
NSString *dateStr = [df stringFromDate:datePicker.date]; 
[btn setTitle:dateStr forState:UIControlStateNormal]; 

} 
+0

非常感謝基爾提mali.Thanks很多!最後它的工作! :) – happycoder