Deleting rows from a static UITableView at runtime - ios

Is it possible to delete rows from a UITableView at runtime when all of the sections and rows are defined statically in a nib?
In the following storyboard scene, can I delete the "Addr 2" field at runtime? In this case I have not provided a data source to the UITableView.

I don't know about "delete" but you can hide the row using tableView:heightForRowAtIndexPath:.
- (void)methodToToggleAddr2CellVisibility
{
self.addr2CellIsHidden = !self.addr2CellIsHidden;
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1 && indexPath.row == 1) { // the cell you want to hide
if (self.addr2CellIsHidden == YES) {
return 0;
} else {
return 44;
}
}
return 44;
}

Related

Expand/Collapse table row when multiple number of sections in tableview

Here i am trying to create expand/collapse table row.it is working fine with this code in didSelectRowAtIndexPath only for 1 section:
if (selectedIndex == indexPath.row) {
selectedIndex = -1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]withRowAnimation:UITableViewRowAnimationFade];
return;
}
//for table view collapse
if (selectedIndex != -1) {
NSIndexPath *prePath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
selectedIndex = (int)indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:prePath, nil]withRowAnimation:UITableViewRowAnimationFade];
}
//for non selection
selectedIndex = (int)indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]withRowAnimation:UITableViewRowAnimationFade];
through this code i can expand and collapse table row but if and only if for 1 section in table but when multiple section comes it is expanding each sections particular row.so when i click on section 0's 1st row it is going to open all section's 1st row.
How to get rid of this situation?
Tapping on particular section button you can change number of rows.
In section view you have added one button for all section which has common method while tapping.
In tapping method you have to just change bool variable value for particular section
ex.. if sender tag you initials with section number.
So For Section 0 button is taped then in tapping method
if sender.tag == 0
{
if section0tag
{
section0tag = false
}
else
{
section0tag = true
}
table.reload() ..//Which call number of rows at section data source method
}
For number of rowsatSection method data source method
if section == 0
{
if section0tag
{
return 5 //Expanding rows
}
else
{
return 0 //collapsing rows
}
}
Try this method just reload tableview (i.e rOne,rTwo, etc are the bool value):
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return rOne ? oneRoundType.count : 0 ;
}else if (section ==1) {
return rTwo ? twoRoundType.count : 0 ;
}else if (section ==2) {
return rThree ? threeRoundType.count : 0 ;
}else if (section ==3) {
return rFour ? fourRoundType.count : 0 ;
}else if (section ==4) {
return rFive ? fiveRoundType.count : 0 ;
}
return 0;
}
Please try below code:
Global declaration of sectionIndexToBeExpanded as int to check whether it is expanded or not
int sectionIndexToBeExpanded;
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//Assign value 100 to sectionIndexToBeExpanded, if you do not want row expanded
sectionIndexToBeExpanded = 100;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [yourArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (sectionIndexToBeExpanded==100) //If not expanded
{
return 1;
}
else if (sectionIndexToBeExpanded==section) //If section expand add one more row
{
return 2;
}
else
return 1;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row==0)
{
if (sectionIndexToBeExpanded==100) //If section not expanded set value of sectionIndexToBeExpanded to 100
{
UITableViewCell *cell=(UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationRight];
sectionIndexToBeExpanded=(int)indexPath.section;
[tableView endUpdates];
}
else if (sectionIndexToBeExpanded==indexPath.section) // If Section is already expanded at indexpath then collapse ie. delete row
{
UITableViewCell *cell=(UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row+1 inSection:sectionIndexToBeExpanded]] withRowAnimation:UITableViewRowAnimationLeft];
sectionIndexToBeExpanded=100; //Set section to not expanded
[tableView endUpdates];
}
else //Expand section
{
UITableViewCell *cell=(UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row+1 inSection:sectionIndexToBeExpanded]] withRowAnimation:UITableViewRowAnimationLeft];
//set value of expanded section to sectionIndexToBeExpanded
sectionIndexToBeExpanded=(int)indexPath.section;
[tableView insertRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationLeft];
[tableView endUpdates];
}
}
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}

static uitableviewcells with custom classes not showing content

I'm working with Xcode 6.1.1, and I have a table view controller with static cells having 4 sections.
Section 0: contains two rows, each with 2 labels
Section 1: contains 7 image views
Section 2: contains 1 row with 1 label
Section 3: contains a variable number or rows each containing 2 image
views
I want to load all cells in the table view controller with data.
I gave each different row a reuse identifier (5 reuse identifiers in total), and I also created a class for each of the different kind of cells (5 classes)
I created outlets in the classes, and linked them in the storyboard each to its label/image view.
However I don't know how to proceed next.
If I implement the table view data source functions, I'm getting the correct number of cells, however they are all blank.
Here's how I'm creating the cells:
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
if (indexPath.section == 0 && indexPath.row == 0) {
static NSString *CellIdentifier = #"MyCell1";
MyTableViewCell1 *cell = (MyTableViewCell1*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MyTableViewCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
} else {
//create other cells here
}
cell.label1.text=#"testing 123";
return cell;
}
However, I'm always getting cells with default style (my labels or image views are not appearing)
What am I missing?? What code should I write in the custom table view cell classes?
EDIT: After gavin's suggestion, I modified the delegate function to use the dequeueReusableCellWithIdentifier:forIndexPath
I also added: [self.tableView registerClass:[MyTableViewCell1 class] forCellReuseIdentifier:#"MyCell1"] to my viewdidload method.
Here's my final code:
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[MyTableViewCell1 class] forCellReuseIdentifier:#"MyCell1"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 3 ) {
return 1;
} else {
return [super tableView:tableView numberOfRowsInSection:section];
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
if (indexPath.section == 3) {
// make dynamic row's cell
static NSString *CellIdentifier = #"MyCell1";
MyTableViewCell1 *cell = (MyTableViewCell1*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell configureCellWithValues:#"17" andV2:#"13"];
return cell;
} else {
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 3) {
MyTableViewCell1 *c = (MyTableViewCell1 *) cell;
NSLog(#"willdisplay cell with values %#, %#",c.i1, c.i2);
[c.image1 setImage:[UIImage imageNamed:#"2"]];
}
}
And here's my custom cell's class:
#implementation MyTableViewCell1
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void) configureCellWithValues: (NSString*) v1 andV2:(NSString*) v2 {
self.i1=v1;
self.i2=v2;
self.image1.image = [UIImage imageNamed:v1];
self.image2.image = [UIImage imageNamed:v2];
}
#end
The cells in section 3 are still not showing the images (not even the imageviews)
Look like you're using the old method of tableView reusable cell dequeueing - the new function accepts an indexPath as well as the cell identifier; it also now guarantees to return a cell (so no need for a nil check).
Here's your delegate function cleaned up:
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
if (indexPath.section == 0 && indexPath.row == 0) {
static NSString *CellIdentifier = #"MyCell1";
MyTableViewCell1 *cell = (MyTableViewCell1*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
} else {
//create other cells here
}
cell.label1.text=#"testing 123";
return cell;
}
Another tip would be to encapsulate your setting of the label values into each individual cell subclass, e.g. add a method called configure to MyTableViewCell1 that sets the label values to what you want displayed.
It seems that we can't generate custom static cells and try to access their outlets and change them (because their static) please correct me if i'm wrong because I couldn't access them at all.
I've solved my Problem by changing the TableView Type to Dynamic Cells, i didn't put any prototype cells in the tableview and I have created 3 different Cell Classes (with their nibs)
I have registered the nib files with the tableview by adding this to the viewdidload method:
[self.tableView registerNib:[UINib nibWithNibName:#"MyCellType1" bundle:nil] forCellReuseIdentifier:#"CellType1"];
[self.tableView registerNib:[UINib nibWithNibName:#"MyCellType2" bundle:nil] forCellReuseIdentifier:#"CellType2"];
[self.tableView registerNib:[UINib nibWithNibName:#"MyCellType3" bundle:nil] forCellReuseIdentifier:#"CellType3"];
then i fully customized my tableview methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
switch (section) {
case 0:
return 2;
break;
case 1:
return 1;
case 2:
return [self.myarray count];
default:
break;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case 0: {
MyCellType1 *cell = (MyCellType1 *) [tableView dequeueReusableCellWithIdentifier:#"CellType1" forIndexPath:indexPath];
//this section always has 2 rows
if (indexPath.row==0) {
cell.lblTitle.text = #"ID";
cell.lblValue.text = [NSString stringWithFormat:#"%i",1277];
} else {
cell.lblTitle.text = #"Date";
cell.lblValue.text = #"02/03/2015";
}
return cell;
}
break;
case 1: {
MyCellType2 *cell = (MyCellType2 *)[tableView dequeueReusableCellWithIdentifier:#"CellType2" forIndexPath:indexPath];
cell.image1.image = [UIImage imageNamed:#"3"];
cell.image2.image = [UIImage imageNamed:#"4"];
return cell;
}
case 2: {
MyCellType3 *cell = (MyCellType3 *)[tableView dequeueReusableCellWithIdentifier:#"CellType3" forIndexPath:indexPath];
cell.lblName.text = #"New Row";
return cell;
}
default:
break;
}
return nil;
}

UITableViewSection Row Indices

I have a UITableView with three sections (Friday, Saturday, Sunday) each with different data and a different number of rows. When selected, the selected row expands, but so do the rows in the other two sections with the same index. I only want the single selected row to expand.
Is there a way to also get the the section in my didSelectRowAtIndexPath method, and to use that in my heightForRowAtIndexPath method in order to keep all of the other rows at their unexpanded size?
The methods below are both in my UITableView subclass:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
customCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//Close row if tapped a second time
if(self.currentSelection == indexPath.row) {
self.currentSelection = -1;
}
//Otherwise, expand row
else {
self.currentSelection = indexPath.row;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
int rowHeight;
if ([indexPath row] == self.currentSelection) {
rowHeight = 185;
} else rowHeight = 44;
return rowHeight;
}
Check NSIndexPath. This object contains section as property.
Save the index path as your selection instead of just the row. At the moment you are saving, say, row 2, and each section has a row 2, so that row is expanding in all sections.
You can compare index paths as follows:
if([indexPath1 isEqual:indexPath2])
// Do your thing
Better yet, UITableView has an indexPathForSelectedRow property, so you don't even need your own variable to keep track of this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
int rowHeight;
if ([indexPath isEqual:tableView.indexPathForSelectedRow]) {
rowHeight = 185;
} else rowHeight = 44;
return rowHeight;
}

Cell hidden property [tableView]

I have section:1 has 11 rows. I set height like this
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) return 350;
else if (indexPath.section == 1) {
return 150;
}
else return 0;
}
if I set cell.hidden = YES; how can I remove the cell because if cell.hidden = YES it will be show space but I don't want that space I want if the cell is hidden directly remove from the secrion:1
or if I can set height for every hidden cells for 0.0
Thanks
EDIT
there is no NSArray for tableView it is customized like this
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) { return 1; }
else if (section == 1) {
int retype = [type intValue];
if (retype == 1 || retype == 5 || retype == 10) {
return 0;
}
else if (retype == 11) { return 9; }
else if (retype == 14) { return 4; }
else {
return 11;
}
}
else return 0;
}
and for the cell configure like this
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
int truef = [*nsstring* intValue];
if (indexPath.section == 1) {
if (indexPath.row == 0) {
if (truef == 0) {
cell.hidden = YES;
}
else {
cell.titleLabel.text = #"True";
}
}
}
EDIT 2
This the error after use deleteRowsAtIndexPaths
reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (11) must be equal to the number of rows contained in that section before the update (11), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out)
You don't need to hide cell. The appropriate way here is to remove this cell from your dataSource and remove it from table view via
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
EDIT
Well... you will agree with me that "magical numbers" which you are using is not good way.
I see the following solution here: you put all your numbers which you return in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
into instance variables, like
#property (assign, nonatomic) numberOfRowsInSection0;
than you can decrease this variable and remove the LAST row. In you situation you will be able remove only last row from table view.
Conclusion: the solution above just a hack which will allow you to remove only last row in your table. You have to review you code again and change logic in order to use array or any other storage. The table view assumes that you have some data source, but you don't, that is the core of you problem.
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if(cell.hidden) { return 0.0f; }

Setting up multiple sections in UITableView

I have setup a uitableview with custom cells.
I would like these into sections with a title. Looking at the below photo I am looking for the following layout:
Section - My Profile
Custom cell - wwwwwwwwwwwwww...
Section - Application
Custom cell - Games
Custom cell - Share
Custom cell - Rate
Custom cell - Settings
Custom cell - Help
Custom cell - Log out
I can see how to add a section and control the rows in a section, but this duplicates the cells into multiple sections, I am not sure how to have one section with one row and another section with 6 rows. I also want to style these sections to show, slightly like the Facebook menu style.
Should I create custom cells for the actual sections instead and have no action on the section (cell) selection?
Here is the code for the UITableView
static NSString *CellIdentifier = #"Cell";
LeftMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"LeftMenuTableViewCell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]]) {
cell = (LeftMenuTableViewCell*)view;
}
}
}
You could define number of sections and rows in it as following:
- (UIView *) tableview:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView view;
if(section == 0) {
// Initialise view for section 1
} else {
// Initialise view for section 2
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ((section == 0) ? 1 : 6);
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// return appropriate cell(s) based on section
if(indexPath.section == 0)
{
// Return 1 cell
}
else if(indexPath.section == 1)
{
switch(indexPath.row) {
case 0: // Initialize cell 1
break;
case 1: // Initialize cell 2
break;
...
}
}
return cell;
}

Resources