2014-10-18 3 views
-1

В моем приложении есть вид навигации (Embed In), контроллер табличного представления и контроллер просмотра. И в ViewController.h есть ...Add Cell в контроллере Table View

@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 reloadData] после [self.tableView endUpdates]; –

+0

@Asif Asif все еще не работает ... –

+0

добавить NSLog (@ "% @", self.titleArray.description) ниже [self.titleArray addObject: self.cellplustitle]; см., если он увеличивается каждый раз, когда вызывается метод plusbtn:. –

ответ

0

вы должны перезагрузить UITableView с помощью

[self.tableView reloadData]; 

Надеется, что это помогает!

+0

Как @Asif Asif сказал, я добавил его между endUpdates amd scrollToRowAtIndexPath, но это не сработало. –

+0

Я установил делегат и источник данных, но все же он не работает. –

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];} 

Надеюсь, это поможет?

Смежные вопросы