Clearing TableView contents - iphone

Can any one help me by providing code for how to clear all the table view cell contents.
The tableview when a button is pressed gets reloaded but does not get cleared
Thanks
Rakesh

For clearing a tableView, you just remove all objects from the array that is populating your table and you reload the tableView after that.
[myArray removeAllObjects];
[self.tableView reloadData];

Set a bool tableIsEmpty and set the number of rows in the tableview to either 0 (table is empty) or the arrays count according to this BOOl in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
Of cousre you have to set tableIsEmpty = YES, when the button is pressed and to NO again when theres data loaded.
Then call
[self.tableView reloadData];
Not tested but should work shouldnt it?

You can just add one variable as flag before reloading the table.
makeEmpty = YES;
[tableview reloadData];
makeEmpty = NO;
Then in the tableview delegate method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(makeEmpty)
return 0;
}

Related

Insert Dynamic Section Headers in UITableView

I have a UITableView in which i want to add the section headers dynamically as the user clicks add button. The user should also be able to delete the headers if they dont need it. Under each header the user should be able to add relevant list of items. And apart from that the user should be able to insert rows dynamically under selected section only. Please suggest some ideas to achieve this functionality. Thanks in advance.
You can achieve this by removing and then adding that section again, this will cause a call to tableView:titleForHeaderInSection:
Assume your section index is 0:
BOOL shouldShowHeader;
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
shouldShowHeader ? #"Your Header" : nil;
}
- (IBAction)buttonAction:(id)sender
{
shouldShowHeader = !shouldShowHeader;
NSIndexSet *set = [NSIndexSet indexSetWithIndex:0];
[self.tableView beginUpdates];
[self.tableView deleteSections:set withRowAnimation:UITableViewRowAnimationNone];
[self.tableView insertSections:set withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
Footer works the same way.
I will tell for dynamic section header. Keep a flag and set to 0 initially. When User clickes on button in that buttonClick method set flag to 1 and reload tableView.
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(flag)
return aView;//alloc init aView and return.
else
return nil.
}
Similarly for dynamic rows have a array and numberOfRows be array.count.
On click of button insert one more item in array and reload Table.
I hope this help.
When user clicks on button in this method set a flag and reload your tableview and then do it..
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(flag)
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 30)];
return headerView;
}
else
return nil;
}
and you can delete header in table view like this......
If you want to remove the table's header view, just set the myTable.tableHeaderView property to nil. If what you have is actually a section header, then you need to return nil from the viewForHeaderInSection method and call [myTableView reloadData]
and i hope this will work fine.

Selecting multiple rows in tableview

I implemented the UITableView in my iPhone application. Further more I added multiple selection of row in UITableView.But the problem is that when I select particular row and then scrolls table and come back then respective row in unselected(unmarked).Here is my didSelectRowAtIndexPath method.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if([tableView cellForRowAtIndexPath:indexPath].accessoryType==UITableViewCellAccessoryNone)
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;
[selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];
NSLog(#"Select now %d",[selectedIndexes count]);
}
else if([tableView cellForRowAtIndexPath:indexPath].accessoryType==UITableViewCellAccessoryCheckmark)
{
[tableView cellForRowAtIndexPath:in dexPath].accessoryType=UITableViewCellAccessoryNone;
[selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]];
NSLog(#"DeSelect now %d",[selectedIndexes count]);
}
}
Please provide me solution.
While you are scrolling the table view every time,the delegate method cellForRowAtIndexPath method is called every time..So,it will load the data every time.Means, first you have to store which rows are clicked in didSelectRowAtIndexPath method, and according to these rows indexes in cellForRowAtIndexPath method you have to show that which table view rows are already selected with check marks and for remaining rows with out check mark...
check this. multiple-row-selection-and-editing
You will find really easy & usefull.
Enjoy Programming

How to hide custom check mark in table view

I have a UITableiew that I want to be able to display a custom check mark when the row is selected (tapped), however, if there is a check mark on any of the other rows, it must be hidden. What is the best way to do this? The IF... doesn't seem to work at all, it will not hide and unhide the check mark as I thought it would.
Thanks in advance.
-PaulS.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
home_2_CustomCell *cell = (home_2_CustomCell*)[tableView cellForRowAtIndexPath:indexPath];
//This will hide the selected row...
cell.imageViewDidSelectRow.hidden = YES;
// if (cell.imageViewDidSelectRow.hidden = NO){
// cell.imageViewDidSelectRow.hidden = YES;
// }
}
1) Maintain a tag in class level as an NSIndexPath variable.
2) Whenever a cell is selected make note of the indexPath and reload the table view.
3) In cellForRowAtIndexPath delegate check for this variable and set marks accordingly.
4) This will not be costly if you have the cell with less information.
You can get the each cell of tableview by this code
UITableViewCell *cell=[product_table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:clickedTag inSection:0]];
make one for loop.
for(int i=0;i<[tabledata count];i++){
cell.imageViewDidSelectRow.hidden = YES;
}
for each cell except the current row which is selected and you got only one image displayed for the current row.

UITableView -> numberOfRowsInSection:(NSInteger)section returns wrong value

My 2nd time today... But here are so many good developers...
Hi,
I have some troubles with my tableView... If the view will appear my table reloads its data, but it doesn't update the numbersOfRowsInSection...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"COUNTER: %i",[content count]);
return [content count];
}
The log says the first time I load the page the right value. But if I add an object and reload the data, this function isn't calling.
Is there anyone who know the solution?
Thanks,
mavrick3.
EDIT:
Heres how I call [table reloadData]
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if ([content count] != 0) {
self.content = [[[[NSArray alloc] initWithContentsOfFile:[FileManager filePath]] objectAtIndex:row_] objectForKey:#"faecher"];
[table reloadData];
NSLog(#"----%#",content);
}
}
And yes, the dataSource and delegate are connected and they are both implemented in my header.
If you call reloadData on a UITableview object and you're it's dataSource then this method should be called called.
Are you sure that you have assigned your table view to your property (i.e. dragged it across in interface builder?)
I suspect that you have only made your controller the dataSource of your table view (hence the first time it loads it gets the value). After that your calls to reloadData won't work because the property in your view controller is still nil.
You can test this by putting an NSLog just before you call reloadData - if it outputs nil then you've not connected it.
NSLog(#"%#", myTable);
[myTable reloadData];
any compiler warnings?
Is "table" actually an instance of UITableView?

problem in reloading tableview

HI all
i am using
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[tableview reloadData];////not working
}
but my table is not reloading.but when i put
[tableview reloadData];
in viewdidload, my data didnt show up.
BUt on rerunning the app, whole data shown up...
i m confused ,what is happeneing here.plz suggest me a proper way to reload table.
Try
[self.tableView reloadData];
check that you have set the delegates properly
In
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [datas count];
}
Make sure that you update the mutable array "datas" with the values.Because only with reference to this array count table cells are created.
All the best.