How to Dynamically Add Rows to a Specific Uitableview Section

How to dynamically add rows to a specific UITableView section?

You can use insertRowsAtIndexPaths: method of UITableView

//Update data source with the object that you need to add
[tableDataSource addObject:newObject];

NSInteger row = //specify a row where you need to add new row
NSInteger section = //specify the section where the new row to be added,
//section = 1 here since you need to add row at second section

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];

Unable to insert new row to UITableView with dynamic number of Sections And Rows

As the error message tells you, you are trying to insert more rows, but what your array is telling you is that you need more sections. You should therefor use:

tableView.insertSections(sections: IndexSet, with: UITableViewRowAnimation)

How to add number of rows in section dynamically in tableview?

Let's say you have an NSMutableArray of this data like this:

NSMutableArray *cellLabelValues = [[NSMutableArray alloc] initWithObjects: @"1",@"2",@"3",nil];

In your tableView delegates do this:

- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return cellLabelValues.count;
}

You must have a button or something to receive user's intent to add new cells. Lets assume it calls this method:

   -(void) addNewCell:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter New Value"
message:@" "
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}

And write the delegate of UIAlertView. Make sure you have made your ViewController to be UIAlertViewDelegate:

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
NSString *value = [alertView textFieldAtIndex:0].text;
[cellLabelValues addObject:value];
[tableView reloadData];
}
}

What happens here is, user taps new cell button. It takes entry from user. Then it reloads the table after adding it to your array. When it reaches numberOfRowsInSection delegate method, it sees that the array is now incremented by 1. So it will show one extra cell.

How to add data dynamically to UITableView when tapped on section headers

You should redesign your model.
Model must be reflective of you UI. As section contains N number of rows so your model of section should have an Array of Row Model. So you could easily file the list of rows for particular Section. Managing Section & Row in two different Array is a headache to manage.

For Example.

struct SectionModel {
var opened: Bool!
var yourTableRowModels = [RowModel]()
}

struct RowModel {
var someAttribute: String!
}

Now in your TableViewDataSource methods use below approach.

class YourViewController: UIViewController {
var sections = [SectionModel]()

func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].yourTableRowModels.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let rowModel = sections[indexPath.section].yourTableRowModels[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "dataCell") as! DataCell
cell.chapterName.text = rowModel.someAttribute
cell.numberOfDocumentsAndAssignments.text = rowModel.someAttribute
return cell
}
}

I want to made Uitableview with dynamic section with dynamic rows

Based on the comments you can change the api response into array and simply access with index. The json response will look like this

    {
"data" : [
{
"month" : "January",
"details" : [
{
"date" : "20-jan-18",
"subtitle" : "Only four days remaining",
"title" : "School fees of August, 2018School fees of August, 2018"
},
{
"date" : "21-jan-18",
"subtitle" : "Holi",
"title" : "Holiday on third march"
}
]
},
{
"month" : "february",
"details" : [
{
"date" : "20-feb-18",
"subtitle" : "Paricipate in this activity",
"title" : "Annual function will be held in feb,2018"
},
{
"date" : "20-12-18",
"subtitle" : "Holiday issue by Govt. of India",
"title" : "Bharat Band"
}
]
},
{
"month" : "march",
"details" : [
{
"date" : "20-feb-18",
"subtitle" : "Paricipate in this activity",
"title" : "Annual function will be held in feb,2018"
},
{
"date" : "20-feb-18",
"subtitle" : "Paricipate in this activity",
"title" : "Annual function will be held in feb,2018"
}
]
}
]
}

Now you can access the first object from the json array and then retrieve the respective key and display it. It will be something like

let object = response.data![index]
object.month // "January"

Hope it helps

Add rows to existing UITableView section

You have to create an array of indexpaths as -

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

for example if you want to insert 2nd row in section 0 and already have 1 row in section 0 then create an index path for row 1 in section 0 and call the insertRowsAtIndexPaths on table view, it will insert 2nd row in section.

If there is no section in table view then you should use an int for data source to give no of sections in table view, in this use -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return noOfSection; //here section is as int which is initially zero
}

and initially your int "noOfSection" will be zero then there will be no section in table, then when you want to add section increase your int value by 1 and call [tableView reloadData];

How to perform a dynamic creation of sections and cells to a UITableView in SWIFT 3

For your information numberOfRowsInsection exists in swift3, see below

 override func numberOfSections(in tableView: UITableView) -> Int {
return section.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items[section].count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
cell?.textLabel?.text = items[indexPath.section][indexPath.row]
return cell!
}

Thanks:)

How to deal with dynamic Sections and Rows in iOS UITableView

An approach I use when creating a table where I know all the possible sections is with enums. You create an enum for each of the possible section types:

enum SectionTypes { case sectionA, sectionB, sectionC }

And then create a variable to hold the sections:

var sections: [SectionTypes]

When you have your data ready then you populate sections with the sections that needs to be displayed. I usually also make a method to help get the section:

func getSection(forSection: Int) -> SectionTypes {
return sections[forSection]
}

With this in place you can start implementing the common DataSource delegate methods:

func numberOfSections(in collectionView: UICollectionView) -> Int {
return sections.count
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
switch getSection(forSection: section) {
case .sectionA:
return 0 //Add the code to get the count of rows for this section
case .sectionB:
return 0
default:
return 0
}
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch getSection(forSection: indexPath.section) {
case .sectionA:
//Get section A cell type and format as your need
//etc
}
}

Trying to add a new row dynamically to a UITableView in iOS

You need to insert an additional row into your table data array and then call insertRowsAtIndexPaths on your tableview to let the table view know about the new row. The new row will be at the end of the array, so row count-1.

[self.tableData addObject:newObject];
NSIndexPath *newPath=[NSIndexPath indexPathForRow:self.tableData.count-1 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[newPath] withRowAnimation:UITableViewRowAnimationAutomatic];


Related Topics



Leave a reply



Submit