在我的应用程序中,有一个导航视图(嵌入式),一个表视图控制器和一个视图控制器。在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 :(得分:1)
ReloadData应该可以工作。如果你把它放在beginUpdate和endupdate之间它不起作用,那么注释rest代码并在数组中添加item后简单地尝试重新加载表视图。喜欢......
- (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];}
希望这有帮助吗?
答案 1 :(得分:0)
您必须使用
重新加载uitableview[self.tableView reloadData];
希望它有所帮助!