开发者

iPhone tableview does not create cell for added row

开发者 https://www.devze.com 2022-12-20 12:55 出处:网络
I have a SQLite DB containing tasks. Each task has a date, and there can be multiple tasks per date. My app loads the data for one month only into a mutable dictionary. The key of each dictionary ite

I have a SQLite DB containing tasks. Each task has a date, and there can be multiple tasks per date.

My app loads the data for one month only into a mutable dictionary. The key of each dictionary item is a string of the day of the month (1, 2, 3 ... 31). The corresponding value is an array of tasks for that date. Thus, for each day of the month, there is an array of 0 or more tasks.

The mutable dictionary is declared in the root viewcontroller. The root viewcontroller in Interface Builder is the dataSource and delegate of the table view. Somehow the app knows that the root viewcontroller ge开发者_运维技巧ts its data from the mutable dictionary. I don't specify this, so I wonder what would happen if there were more than one dictionary, or maybe a dictionary and a mutable array? Nonetheless, this works without additional wiring. When the root viewcontroller loads the data into the table, each section header holds the date as its text, and lists all the tasks in individual cells underneath. Some headers have no cells.

Now, to modify a task, I have an "entry" viewcontroller. Selecting a task in the table causes the entry viewcontroller to load that task info into text fields, where I may change the title, date, details, or mark it as done. After editing, the entry viewcontroller unloads, the root (table) viewcontroller reloads, the view's tableView calls reloadData and the changes are apparent.

HERE'S THE PROBLEM: if I ADD a new task by clicking the + button on the nav controller, it takes me to the same entry viewcontroller, where I set the data. I return to the root viewcontroller via the same methods as when editing existing data. Yet no new cell has been created for the new data! If I close and reopen the app, the new data displays correctly.

Since data updates and inserts are written to the DB from the dictionary's arrays when the app closes, this means that the new item really was added to one of the arrays.

Summary: edit existing array item, and the new data is displayed when the table reloads its data. Add an array item, and it is ignored when the table reloads.

What's wrong?


You might add a line like this in your UITableViewController:

[self.tableView reloadData];

This will cause the entire UITableView to be reloaded. However, I believe the preferred method is:

[self.tableView insertRowsAtIndexPaths: indexPaths withRowAnimation: YES];

For this to work, you need to know where the row(s) will appear (the array of NSIndexPaths).

It may be easier to update the UITableViewController from within the "entry" ViewController.

0

精彩评论

暂无评论...
验证码 换一张
取 消