How to collapse my cell properly? - ios

while i click the first cell its working properly and hiding correctly.
while i click second cell,The backend cell has been merged and displaying improperly.
whats wrong in this code please find out what is wrong in this code.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
#pragma mark table cell creating and loading the data
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *returncell;
AntzclubCell *cell;
WaterPurifierCell *cell1;
if(indexPath.row==0)
{
cell=[tableView dequeueReusableCellWithIdentifier:#"Antz"];
cell.img_antzClub.image=[UIImage imageNamed:#"car.png"];
cell.lbl_antzClub.text=#"CAR";
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
cell.backgroundColor=[UIColor blackColor];
return cell;
}
else if (indexPath.row==1)
{
cell1=[tableView dequeueReusableCellWithIdentifier:#"WaterPurifier"];
cell1.img_waterPurifier.image=[UIImage imageNamed:#"water_purifier.png"];
cell1.lbl_waterPurifier.text=#"WATERPURIFIER";
return cell1;
}
return returncell;
}
#pragma mark expanding height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row) {
case 0:
if(indexPath.row==selectindex)
{
return 350;
}
else
{
return 132;
}
break;
case 1:
if(indexPath.row==selectindex)
{
return 333;
}
else{
return 132;
}
default:
break;
}
return 0;
}
#pragma mark user selecting option
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%d %d",selectindex,indexPath.row);
if (indexPath.row==selectindex) {
NSLog(#"%d",selectindex);
selectindex=-1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
return;
}
if(selectindex !=-1)
{
NSIndexPath *prepath=[NSIndexPath indexPathForRow:selectindex inSection:0];
selectindex=indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:prepath] withRowAnimation:UITableViewRowAnimationFade];
return;
}
selectindex=indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

If you are trying to hide portions of the cell's content by shrinking its height, you'll need to check that the cell is set up to clip to its bounds (by default this is set to NO).
Select your prototype cell in the storyboard, and tick the "Clip Subviews" checkbox - the cell should then clip its contents when you shrink the height.

Related

Can any one tell how to implement expandable/collapse tableview

Here is my screen and code i have attached below.can any one give idea how to implement the screen thanks in advance..in my header section i need show only the tableview cell.........................................
[1]: http://i.stack.imgur.com/EVlQs.png
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return [arr count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellidentifier =#"cell";
MyOrderCell *cell = (MyOrderCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
/********** If the section supposed to be closed *******************/
if(cell==nil)
{
cell = [[MyOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
}
if (selectedindex==indexPath.row) {
// do expanded stuff here
cell.lblcope.text=[[pendentsdata objectAtIndex:indexPath.row]valueForKey:#"pendents"];
cell.lblcopieces.text=[[pendentsdata objectAtIndex:indexPath.row]valueForKey:#"copiece"];
cell.lblconum.text=[[pendentsdata objectAtIndex:indexPath.row]valueForKey:#"conum"];
cell.lblcodate.text=[[pendentsdata objectAtIndex:indexPath.row]valueForKey:#"codate"];
cell.lblcoenddate.text=[[pendentsdata objectAtIndex:indexPath.row]valueForKey:#"coenddate"];
cell.lbltotl.text=[[pendentsdata objectAtIndex:indexPath.row]valueForKey:#"cototal"];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
}else
{
// do closed stuff here
cell.lblpendnts.text=[arr objectAtIndex:indexPath.row];
cell.lblpieces.text=[arrpieces objectAtIndex:indexPath.row];
cell.lblnum.text=[arrnum objectAtIndex:indexPath.row];
cell.lbldate.text=[arrdate objectAtIndex:indexPath.row];
cell.lblenddate.text=[arrenddate objectAtIndex:indexPath.row];
cell.lbltotl.text=[arrttl objectAtIndex:indexPath.row];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (selectedindex==indexPath.row) {
selectedindex=-1;
[tbldata reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
return;
}
if (selectedindex !=-1) {
NSIndexPath *prevpath=[NSIndexPath indexPathForRow:selectedindex inSection:0];
selectedindex=indexPath.row;
[tbldata reloadRowsAtIndexPaths:[NSArray arrayWithObject:prevpath] withRowAnimation:UITableViewRowAnimationFade];
}
selectedindex=indexPath.row;
[tbldata reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (selectedindex==indexPath.row) {
return 100;
}else
{
return 50;
}
}
You can use https://github.com/iSofTom/STCollapseTableView
I have used this in a project. The code written is very clean, so you can also easily customise it to cater your specific needs if any.
You can use SubTable, a third party subclass of UITableView. Besides same questions as yours are below, check them out:
How to create an Accordion with UItableview under a UItableview?
Accordion table cell - for expand and collapse ios
Indeed Burak, or take tutorial...
an example: https://blog.pivotal.io/labs/labs/expandable-uitableviewcells

How to "shrink" all the cells in TableView and expand the one clicked on

When you click on a cell inside a TableView, it will expand.
How I achieved this was with the following methods:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.topicsArray containsObject:indexPath])
{
[self.topicsArray removeObject:indexPath];
}
else
{
[_topicsArray addObject:indexPath];
}
[_tableView beginUpdates];
[_tableView endUpdates];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat kExpandedCellHeight = 240;
CGFloat kNormalCellHeigh = 115;
if ([_topicsArray containsObject:indexPath])
{
return kExpandedCellHeight;
}
else
{
return kNormalCellHeigh;
}
}
What I cannot figure out how to do is - How to 'shrink' all the cells in a TableView before I expand the one clicked on?!
aha i got it you dont remove the index from the array. try [topicsArray removeAllObjects] before
if ([self.topicsArray containsObject:indexPath])
{
[self.topicsArray removeObject:indexPath];
}
else
{
[_topicsArray addObject:indexPath];
}

UITableViewCells disappears randomly on drag after resize another row

I have a UITableView. I use this code to resize the selected row:
-(void) viewWillAppear:(BOOL)animated {
....
[self.tableView registerNib:[UINib nibWithNibName:#"ICSceneDetailViewTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:IC_CELL_SCENE_EDITOR_CELL];
...
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == nil)
return 0;
if (self.scenes != nil) {
return self.scenes.count;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ICSceneDetailViewTableViewCell *cell = (ICSceneDetailViewTableViewCell*)[tableView dequeueReusableCellWithIdentifier:IC_CELL_SCENE_EDITOR_CELL forIndexPath:indexPath];
if (cell == nil) {
cell = [[ICSceneDetailViewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IC_CELL_SCENE_EDITOR_CELL];
}
ICScene *scene = [self.scenes objectAtIndex:[indexPath row]];
[cell.titleTextField setText:scene.title];
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedRow = indexPath.row;
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath row] == self.selectedRow) {
return 250;
}
if (IS_IPAD) {
return 80;
}
else {
return 40;
}
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
BUT, after selecting any row (name it A, and A become resized):
if I drag another row (name it B) then randomly other rows disappears (except A).
if I scroll tableview and back to drag on same row (B) then, again randomly, other(s) cell(s) disappear(s), but not always the same(s).
if I comment heightForRowAtIndexPath then drag behavior is normal, but row can't be resized :(
I already searched many similar answers (adjusting selectedBackgroundView on cell, backgroundView cell, drawRect on cell, etc) but no one solution/workaround works for this simple (i think) code.
Thanks in advance :)
Yeh, answering my own question.
I found an alternative to resize selected UITableViewCell without calling to reloadRowsAtIndexPaths.
Replacing reloadRowsAtIndexPaths with:
[self.tableView beginUpdates];
[self.tableView endUpdates];
refreshes visible rows without calling cellForRowAtIndexPath. Imho the problem where dequeuing cells on every row being reloaded.
The resulting code is:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedRow = indexPath.row;
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
So, automagically there isn't any disappearing row :)

How can I replace selected customTableCellA with customTableCellB

How can I replace selected customTableCellA with customTableCellB
I already have a table view working with customTableCellAs
I want to replace customTableCellA with customTableCellB when it is selected.
customTableCellB is 50 pixels taller than customTableCellA
How can I implement this properly?
Try this,
Create an #property array NSMutableArray *selectedIndexPathArray and instantiate it in viewDidLoad, then
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat cellHeight = //customTableCellA height
if ([self.selectedIndexPathArray containsObject:indexPath]) {
cellHeight = //customTableCellB height
} else {
cellHeight = //customTableCellA height
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
if ([self.selectedIndexPathArray containsObject:indexPath]) {
//Load customTableCellB
} else {
//Load customTableCellA
}
//Other code
...
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.selectedIndexPathArray containsObject:indexPath]) {
//If you want to remove the selection in second touch, do
//[self.selectedIndexPathArray removeObject:indexPath];
} else {
[self.selectedIndexPathArray addObject:indexPath];
}
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNonne];
//Select appropriate UITableViewRowAnimation
[tableView endUpdates];
}

Add and delete rows dynamically in multiple sections

I have searched and search and i just canĀ“t seem to find an answer to my problem. I have a dynamic tableview with 3 rows (each row is a section) and a edit button at the top right of the tableview. each time the user taps edit it has to be possible to add or delete a row. Everything works except the part when the + button is taped to ad a new row. This is my code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int count = [myarray count];
if (myarray != nil) count ++;
return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
id cell;
switch(indexPath.section) {
case 0:
if(indexPath.row==0) {
static NSString *cellType1 = #"cellType1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellType1];
return cell;
}
break;
case 1:
if(indexPath.row==0) {
static NSString *cellType2= #"cellType2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellType2];
return cell;
}
break;
case 2:
if(indexPath.row==0) {
static NSString *cellType3= #"cellType3";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellType3];
return cell;
}
break;
default:
break;
}
return cell;
}
- (IBAction) EditTable:(id)sender
{
if(self.editing)
{
[super setEditing:NO animated:NO];
[Table setEditing:NO animated:NO];
[Table reloadData];
[self.navigationItem.rightBarButtonItem setTitle:#"Edit"];
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[Table setEditing:YES animated:YES];
[Table reloadData];
[self.navigationItem.rightBarButtonItem setTitle:#"Ok"];
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone;
if (self.editing && indexPath.row == ([myarray count]))
{
return UITableViewCellEditingStyleInsert;
} else
{
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}
- (void)tableView:(UITableView *)TableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[myarray removeObjectAtIndex:indexPath.row];
[Table reloadData];
} else if (editingStyle == UITableViewCellEditingStyleInsert)
{
switch(indexPath.section) {
case 0:
if(indexPath.row==0) {
static NSString *cellType1 = #"cellType1";
UITableViewCell *cell = [TableView dequeueReusableCellWithIdentifier:cellType1];
[arry insertObject:cell atIndex:[myarray count]];
[Table reloadData];
}
break;
case 1:
if(indexPath.row==0) {
static NSString *cellType2= #"cellType2";
UITableViewCell *cell = [TableView dequeueReusableCellWithIdentifier:cellType2];
[arry insertObject:cell atIndex:[myarray count]];
}
break;
case 2:
if(indexPath.row==0) {
static NSString *cellType3= #"cellType3";
UITableViewCell *cell = [TableView dequeueReusableCellWithIdentifier:cellType3];
[arry insertObject:cell atIndex:[myarray count]];
}
break;
default:
break;
}
}
As i said before, everything works until i press the + button (that appears on the left side when the edit button is pressed) to add a new row. Then it shows an error: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath.
What am i doing wrong? any help would be most appreciated.
First off, you never actually +alloc or -init a cell, so -cellForRowAtIndexPath: is most likely returning nil (-dequeueReusableCellWithIdentifier: doesn't cut it).
Second off, comparing indexPath.row to [myarray count] will never be true, because arrays and tables may be zero-based, but their counts aren't.

Resources