메소드에서 내 app의 tableview를 업데이트하려고합니다. 테이블은 데이터 소스로 배열 allShoppingItems를 사용합니다. 그러나 어떤 이유로 새로운 배열을 배열에 추가 할 수 없습니다. 여기에 메서드가 있습니다. ShoppingListTableViewController에 있습니다.IOS의 메소드에서 테이블 뷰 업데이트
- (void)addToShoppingList: (FoodItem *) newItem
{
if (newItem != nil) {
[self.allShoppingItems addObject:newItem];
[self.tableView reloadData];
}
}
필자는 전달 된 개체가 nil이 아니거나 tableView도 아닌지 테스트했습니다.
EDIT : 여기도 ShoppingListTableViewController에있는 numberOfRowsInSection 및 cellForRowAtIndexPath 메서드가 있습니다.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self.allShoppingItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"shoppingListIdentifier" forIndexPath:indexPath];
// Configure the cell...
FoodItem *shoppingItem = [self.allShoppingItems objectAtIndex:indexPath.row];
cell.textLabel.text = shoppingItem.itemName;
return cell;
}
배열은 기본보기 컨트롤러의 viewDidLoad 메서드에서 초기화됩니다. 다음은 모두 AllItemsTableViewController - 초기 뷰 컨트롤러입니다.
- (void)viewDidLoad {
[super viewDidLoad];
self.allFoodItems = [[NSMutableArray alloc]init];
shoppingList = [[ShoppingListTableViewController alloc] init];
shoppingList.allShoppingItems = [[NSMutableArray alloc]init];
}
그런 다음 해당 클래스의 unwind 메서드에서 호출됩니다.
- (IBAction)unwindToAllItems:(UIStoryboardSegue *)segue
{
AddNewItemViewController *source = [segue sourceViewController];
FoodItem *item = source.foodItem;
if (item != nil) {
[self.allFoodItems addObject:item];
[self.tableView reloadData];
}
if (item != nil && item.currentNumber<=item.lowLevel) {
[shoppingList addToShoppingList:item];
}
}
배열은 초기화되었지만 비어 있습니다. 아무리 시도해도 addToShoppingList는 배열에 아무 것도 추가하지 않습니다. NSLog(@"%@",self.allShoppingItems);
으로 인쇄하고 항상 표시되는 것은 모두 "()"입니다.
아마도이 방법을 사용하는 것이 더 쉽습니다. 난 그냥 테이블보기에 개체를 추가 할 수 있고, 특정 조건이 충족되면 두 번째뿐만 아니라 추가 할 수 있습니다.
설명은 확장 토론이 아닙니다. 이 대화는 [채팅으로 이동되었습니다] (http://chat.stackoverflow.com/rooms/67459/discussion-on-question-by-scott-crater-updating-a-table-view-from-a-method- in-io). – Taryn