Я пытаюсь заполнить UITableView из NSMutableArray. У меня есть UITextField и кнопка на ViewController. Когда я набираю текст в UITextField и нажимаю кнопку, я могу видеть, что текст добавляется в массив с помощью NSLog. Я установил точки останова в методе источника данных UITableView, но даже не ударил эти точки останова, когда я нажимаю кнопку.Заполнение UITableView с нажатой кнопки
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
{
NSMutableArray *arrBarcode;
IBOutlet UITextField *txtInsert;
}
@property IBOutlet UITableView *myTableView;
-(IBAction)btnPressed:(id)sender;
@end
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
arrBarcode = [[NSMutableArray alloc]init];
}
-(IBAction)btnPressed:(id)sender{
[arrBarcode addObject:txtInsert.text];
NSLog(@"array count is : %i", [arrBarcode count]);
[self.myTableView reloadData];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[txtInsert resignFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView: (UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if ([arrBarcode count] == 0){
return 0;
}
else{
NSLog(@"Number of Rows : %i", [arrBarcode count]);
return [arrBarcode count];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell Identifier";
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *barcode = [arrBarcode objectAtIndex:[indexPath row]];
[cell.textLabel setText:barcode];
return cell;
}
@end
Когда я инициализировать NSMutable массив с некоторыми данными в методе viewDidLoad, то UITableView заполняемых хорошо, но не с динамическим массивом. Я новичок в Objective C, может ли кто-то указать мне в правильном направлении?
Моя проблема была в раскадровке, я создал свойство myTableView, но я не подключил myTableView с представлением таблицы в контроллере Outlets for View. Когда кнопка нажата, я реализовал resignFirstResponder. Прекрасно работает! – Pramesh
Так что проблема с раскадрой была проблемой, как и предполагалось. Вы должны принять этот ответ. – Mundi