Your Second iOS app Tutorial: How Many Sections Do They Want? - ios

In Your Second iOS app, there is a portion that says the following:
After you finish laying out the cells in the table, the detail scene should look similar to this:
However, mine has Section-1, Section-2, Section-3 right above each section. I get the feeling that they want 1 section with three cells.
Can you tell which?

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
This should be your code to have 3 rows and only 1 section like that of image

You incremented the number of sections by mistake instead of incrementing the number of rows. Which you should have done.
Edited to add
You get to this by highlighting the Table View Section in the controller scene and editing the number of rows in the right hand panel.
I picture makes this clearer:

Yes, according to the image its 1 section with 3 rows. In numberOfSections method return 1 and in numberOfRowsInSection method return 3.

They are looking for one section with three cells.

Related

n view controllers or n cells

I have a scenario where the user is transformed to a new view controller vc2 when he presses add button.
depending on where he came from, required fields will change. For instance, if he choose to add a task he should enter title, due date and time, but when he choose to add a person he should enter contact info.
Is it better to have two separate view controllers (vc2a,vc2b) or one table view controller vc2 that contains prototype of all cells for the two case, but every time I call cellForRowAtIndexPath, I have to check from where I come.
for me as a developer it is easier to have different view controller in storyboard especially when n>2, but if that can make a difference in performance things will change.
Thanks in advance.
As per my opinion it is better to do this task in a single view controller, provided you are performing similar next action after adding a task of adding a person.
And instead of putting up the checks in cellForRowAtIndexPath because it is called multiple times do place a check in numberOfRowsInSection.
In numberOfRowsInSection do
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
OMGOrderDetailsSectionType sectionType = [[self.tableViewLayoutArray objectAtIndex:section] integerValue];
if (sectionType == 0) {
if(self.isContactsType) {
return 1;
}
}
else if (sectionType == 1) {
if(self.isTaskType) {
return 1;
}
}
return 0;
}
Rest remains same for cellForRow, do configure both type of cells.

Error updating UITableView after smaller sized JSON response?

Currently, I have my app hitting my endpoints, and getting back 10 or less items. In the event the items returned is less than 10, and the UITableView is already showing 10 items, reloadData() will cause an error because the size is not the same as it was last time. Right now, when I get my response all I do is:
tableView.beginUpdates()
self.items = items //where self.items is the array that backs the UITableView, and items are the items I got back in form of JSON from the server.
tableView.reloadData()
tableView.endUpdates()
Why will it cause error? If you are using tableView.reloadData(), you don't need to call endUpdates() and beginUpdates(). Just simply assign the items and reload tableView data.
self.items = items
tableView.reloadData()
If you are on the background thread call the above code on the main queue.
dispatch_async(dispatch_get_main_queue()) { () -> Void in
// Code runs on main queue!
}
It is not your calling reloadData that caused error. It is because your numberOfRowsInSection or numberOfSectionsInTableView didn't return right value after you update your model.
In the table view method,
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {}
you should write as , return [self.items count];
This way, your table will know, how many items your table need to show, and reload data would not complain.

Hide a static cell of Tableview in swift

I've an application that works with a static cells table view. In some cases in need to hide a few cells and when needed show them again. How can i do this ?
To do what you wanna do I work with numberOfRowsInSection tableview's method using a bool variabile
if section == 3 {
if !hideTableSection! { return 1 } else { return 2 }
}
Every time I need to hide some cell I change the bool variable then I reload my table.
It can manage only the latest rows of every section but I didn't find nothing better...

NSInternalInconsistencyException when removing rows from a UITableView

I'm receiving an error indicating that I need to update the number of rows in my UITableView after I have deleted a row. I realise that It's because I've not updated the amount of rows in the UITableView, I'm just not sure how to make this change in the code I have below.
Any assistance is really appreciated.
!-- code
NSIndexPath[] rowsToReload = new NSIndexPath[] {
NSIndexPath.FromRowSection(1, 1)
};
dv.TableView.BeginUpdates ();
dv.TableView.DeleteRows(rowsToReload,UITableViewRowAnimation.Automatic);
dv.TableView.EndUpdates ();
!-- error
Name: NSInternalInconsistencyException Reason: Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3)
Your code above is OK. However, you need to call it after you have removed the corresponding items from your data source.
For example, if you are filling your table view from a List<string> (myList) and you wanted to remove the first row:
myList.RemoveAt(0); // remove the item from the list first
NSIndexPath[] rowsToDelete = new NSIndexPath[] {
NSIndexPath.FromRowSection(0, 0)
};
dv.TableView.DeleteRows(rowsToDelete, UITableViewRowAnimation.Automatic);
No need to wrap the DeleteRows call in a Begin/EndUpdates, as you only have one delete action to perform. That is used when you have multiple different actions (eg. DeleteRows + InsertRows) so that the animations are performed smoothly.
#Dimitris thanks for answering my question. I ended up solving the problem by:
calling the reload method on my UITableView
NSIndexPath[] rowsToReload = new NSIndexPath[] {
NSIndexPath.FromRowSection(1, 1)
};
dv.TableView.ReloadRows (rowsToReload,UITableViewRowAnimation.None);
in my UITableViewCell GetCell method
I removed the label, field and button that were included on the first load of the table. Since this screen in my app will be seen only once, when the user loads it for the first time.

Showing/Hiding Rows in Sections of UITableView

I am trying to create a UITableView that is set up so that when I click on a section, it hides the rows from the previous section and shows the rows of the newly selected section. The tableView is set up with data from an array of arrays called menuItems. menuItems contains several other arrays including contactInfoArray, clientInfoArray, jacketArray, and shirtArray. In each of these arrays, the objectAtIndex:0 is the Section Title and effectively acts as the header.
So, for example when the app loads, it should first show all rows of the contactInfoArray, but then show the ObjectAtIndex:0 (the title) for each of the other arrays. When I then tap one of those section titles, for example: "Jacket", I need the table to hide the contactInfoArray objectsAtIndexes:1+, but not object 0, while at the same time (or with an acceptable delay) showing jacketArray objectsAtIndexes:1+.
I have achieved the desired result by calling reload data, but I want animation of the change and reloadData does not allow that. I can't find any tutorials or sample code that does this.
Can anybody help me?
Thanks to my good friend Darren for helping me solve this issue!
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section{
return (indexPath.section == currentSection) ? [[menuItems objectAtIndex: indexPath.section ] count] : 1;
}
All that I needed to do was set an immediate variable (in my case "currentSection" which is an int) and set its value in the didSelectRowAtIndexPath: method. Then I set up my table so that row 0 of the section acted as the section title.
Doing this made it so that when I clicked on the section header it would "collapse" the open section and "expand" the newly selected section.

Resources