Привет, я пытаюсь добавить данные в MutableArray, который работал раньше, когда у меня была кнопка внутри моей таблицы. Но теперь, когда я пытаюсь использовать правую кнопку панели навигации, она не обновляет мою таблицу.Добавить данные в таблицу при нажатии кнопки добавления из моей панели навигации внутри панели вкладок
Кнопка добавлена из моей панели вкладок, после этого у меня есть 4 класса таблицы для каждой вкладки, где я не добавляю данные
Это мой класс Tab Bar.m
@interface TabBar()
@end
@implementation TabBar
@synthesize tab;
MaterialTable *ma;
WorkTable *wo;
MilesTable *dr;
NotesTable *no;
UIBarButtonItem *addButton;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
ma = [[MaterialTable alloc]init];
wo = [[WorkTable alloc]init];
dr = [[MilesTable alloc]init];
no = [[NotesTable alloc]init];
addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];
self.navigationItem.rightBarButtonItem = addButton;
}
// method called when pressing the add button
-(void)add {
if ([self selectedIndex] == 0) {
// method in MaterialTable that adds data to my table
[ma newMat];
}
if ([self selectedIndex] == 1) {
[wo newWork];
}
if ([self selectedIndex] == 2) {
[dr newDrive];
}
if ([self selectedIndex] == 3) {
[no newNotes];
}
}
@end
// КОНЕЦ TAB BAR кЛАССА
// My MaterialTable Class, the first tab in my tab bar
//This is where I wan't to add data to my MutableArray when addbutton is pressed from tab bar
// Method in MaterialTable to add data in my tableview
- (void)newMat {
alertMat = [[UIAlertView alloc] initWithTitle:@"Lägg till material" message:@"" delegate:self cancelButtonTitle:@"Avbryt" otherButtonTitles:@"Lägg till material", nil];
alertMat.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
alertTextFieldMat.autocapitalizationType = UITextAutocapitalizationTypeSentences;
alertTextFieldMat = [alertMat textFieldAtIndex:0];
alertTextFieldMat.keyboardType = UIKeyboardAppearanceDefault;
alertTextFieldMat.placeholder = @"Material";
alertTextFieldAnt = [alertMat textFieldAtIndex:1];
alertTextFieldAnt.keyboardType = UIKeyboardAppearanceDefault;
alertTextFieldAnt.placeholder = @"Antal";
alertTextFieldAnt.secureTextEntry = NO;
[alertMat show];
}
add data to the table
this method is also in MaterialTable class that is called when I press "Lägg till material" which means add material in swedish
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *button = [alertView buttonTitleAtIndex:buttonIndex];
if ([button isEqualToString:@"Lägg till material"]) {
newMat = alertTextFieldMat.text;
ant = alertTextFieldAnt.text;
int i = materials.count;
if (![newMat isEqualToString:@""] && ![ant isEqualToString:@""]) {
NSString *intoArray = [NSString stringWithFormat:@"%@endDate%@endMaterial%@endAmount%dendID", [ac getDateNow], newMat, ant, 0];
// insert data to mutablearray
[materials insertObject:intoArray atIndex:i];
NSIndexPath *myList = [NSIndexPath indexPathForRow:i inSection:0];
[self.tableContent insertRowsAtIndexPaths:@[myList] withRowAnimation:UITableViewRowAnimationAutomatic];
// reload table
[self.tableContent reloadData];
// sets data to my get/set class
[ac setContent:newMat];
[ac setContent2:ant];
[ac setItem:@"material"];
// method running in background adding the data to database
[self performSelectorInBackground:@selector(addData) withObject:nil];
}
}
}
// method called to add my inserted data to the database
-(void)addData {
// add data to the database
// db is a object of class (Database *db)
[db addData];
}
@end
скриншот http://i62.tinypic.com/2d0e5v6.png
Что нужно искать, это способ перезагрузки графики, таблицы или всего класса.
Я использую [self.tableContent reloadData]; Я попробовал [self.view setNeedsDisplay];/[self.tableView setNeedsDisplay];
Он работал раньше, когда я была кнопка в нижней части Tableview чуть выше панели вкладок, который был вызван из того же класса
заранее спасибо
Я решил проблему очень легко. Я делал неправильно все время, потому что я не думал, что я мог бы добавить кнопку с моей вкладки
Я добавил кнопку в каждой вкладке viewDidLoad, как это ->
UIBarButtonItem * AddButton = [[UIBarButtonItem Alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target: self action: @selector (newMat)];
self.tabBarController.navigationItem.rightBarButtonItem = addButton;
Вы разместили ауты печати, чтобы проверить, что все методы вызываются? вы знаете, где он перестает работать, т. е. метод add не вызывается, его не попадает в оператор if и т. д. –
да, я работал, он работает полностью, если вкладка switch и вернуться назад, мои данные там. Поэтому моя проблема заключается в том, что он не будет отображаться сразу после того, как я добавлю его, но он действительно есть, я зарегистрировал массив точно после добавления данных и отображает мои недавно добавленные данные, поэтому я думаю, что ему нужно только графическое обновление в некоторых way –