2014-10-18 76 views
-1

在我的應用程序中,有一個導航視圖(嵌入),一個表視圖控制器和一個視圖控制器。並在ViewController.h中,有...在表視圖控制器中添加單元格

@property (strong, nonatomic) IBOutlet UILabel *timeDisplay; 
@property (strong, nonatomic) IBOutlet UILabel *minuteDisplay; 
@property (strong, nonatomic) IBOutlet UILabel *timerDisplay; 
@property (strong, nonatomic) IBOutlet UILabel *titleLabel; 

和更多的網點。下面的代碼是TableViewController.m文件。我有一個動作按鈕(plusbtn),它需要在表格視圖中添加一個單元格。所以,我試圖讓'plusbtn'做到這一點,但它沒有奏效。我該怎麼辦?請幫我...

#import "TableViewController.h" 

#import "TableViewCell.h" 

#import "ViewController.h" 

@interface TableViewController() 

@property (nonatomic) ViewController *viewController; 
@property (strong, nonatomic) NSMutableArray *titleArray; 
@property (strong, nonatomic) NSMutableArray *tsArray; 
@property (strong, nonatomic) NSMutableArray *tmArray; 
@property (strong, nonatomic) NSMutableArray *ttArray; 
@property (strong, nonatomic) NSString *cellplustitle; 
@property (strong, nonatomic) NSString *cellplustime; 
@property (strong, nonatomic) NSString *cellplusmin; 
@property (strong, nonatomic) NSString *cellplussec; 

@end 

@implementation TableViewController 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    _cellplustitle = self.viewController.titleLabel.text; 
    _cellplustime = self.viewController.timeDisplay.text; 
    _cellplusmin = self.viewController.minuteDisplay.text; 
    _cellplussec = self.viewController.timerDisplay.text; 
    self.titleArray = [NSMutableArray new]; 
    self.tsArray = [NSMutableArray new]; 
    self.tmArray = [NSMutableArray new]; 
    self.ttArray = [NSMutableArray new]; 

    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return self.titleArray.count; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath]; 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    cell.celltitle.text = [self.titleArray objectAtIndex:indexPath.row]; 
    cell.cellmd.text = [self.tmArray objectAtIndex:indexPath.row]; 
    cell.cellsd.text = [self.tsArray objectAtIndex:indexPath.row]; 
    cell.celltd.text = [self.ttArray objectAtIndex:indexPath.row]; 

    // Configure the cell... 

    return cell; 
} 


/* 
// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 
*/ 

/* 
// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 
*/ 

/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 

/* 
#pragma mark - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 
} 
*/ 

- (IBAction)plusbtn:(id)sender { 
    [self.tableView beginUpdates]; 

    [self.titleArray addObject:self.cellplustitle]; 
    [self.tsArray addObject:self.cellplussec]; 
    [self.tmArray addObject:self.cellplusmin]; 
    [self.ttArray addObject:self.cellplustime]; 
    NSIndexPath *indexPathOfNewItem = [NSIndexPath indexPathForRow:(self.titleArray.count - 1) inSection:0]; 
         [self.tableView insertRowsAtIndexPaths:@[indexPathOfNewItem] withRowAnimation:UITableViewRowAnimationAutomatic]; 

         [self.tableView endUpdates]; 
         [self.tableView scrollToRowAtIndexPath:indexPathOfNewItem atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 
} 
@end 
+0

在[self.tableView endUpdates]之後添加[self.tableView reloadData]; – 2014-10-18 16:49:52

+0

@Asif仍然不工作... – 2014-10-18 16:54:46

+0

下面添加NSLog(@「%@」,self.titleArray.description)[self.titleArray addObject:self.cellplustitle];看看它是否每次增加plusbtn:被調用。 – 2014-10-18 17:02:39

回答

0

您在使用

[self.tableView reloadData]; 

希望它可以幫助重新加載的UITableView!

+0

就像@Asif Asif所說,我在endUpdates和scrollToRowAtIndexPath之間添加了它,但它不起作用。 – 2014-10-18 17:02:53

+0

我設置了委託和數據源,但仍然無法正常工作。 – 2014-10-19 13:08:57

1

ReloadData應該可以工作。如果如果你在beginUpdate和endupdate之間放置它,那麼它不起作用,然後註釋其餘的代碼,只需在數組中添加項目後重新加載表視圖。贊...

- (IBAction)plusbtn:(id)sender { 
[self.titleArray addObject:self.cellplustitle]; 
[self.tsArray addObject:self.cellplussec]; 
[self.tmArray addObject:self.cellplusmin]; 
[self.ttArray addObject:self.cellplustime]; 
[self.tableview reloadData];} 

希望這會有幫助嗎?

相關問題