I have a table view and don't know how to access the row number when the user presses one of the cells. The data of the table view is a small array and this is the closest I could come... In the IB, I control dragged the reusable cell to the view controller I push which works fine. I am unsure how to get the row number in a conditional like this...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// A bunch of stuff is before this, but not important to the question
//-- > This line if([indexPath row] == 1) This doesn't work... any ideas?
{
if(totalPoints < 10 && number != 0)
{
[self saveNumber:#"1"];
}
else if (totalPoints < 10 && attachment == 0)
{
[self saveData:#"1"];
int newPnts = totalPoints + 1;
[self savePoints:[NSString stringWithFormat: #"%d", newPnts]];
}
Thanks
It should work
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%#",indexPath.row);
if(indexPath.row==1)
{
NSLog(#"Do Somthing..");
}
else
{
NSLog(#"Do Somthing..");
}
}
the row number is [indexPath row] . in tableview you have a dataSource which save the data array used to reload table
Ok I was doing something wrong elsewhere and the view controller doesn't update it's label from the saved data. I will post a seperate question if you're interested
Related
I have a UITableViewController with static cells, with 3 sections, and a segmented control with 2 buttons.
I would like to achieve the following behavior:
when button 1 is pressed hide section 2
when button 2 is pressed hied section 3
I cannot find a solution to this.
Any tip is useful.
Thanks.
Simple, just make sure you set your UITableViewDelegate, and you can use heightForRowAtIndexPath: (and similar for headers and footers) to show/hide the cells by setting their height to 0.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 2) {
if (self.shouldShowSection2) {
return 44.0f;
}else{
return 0.0f;
}
}else if (indexPath.section == 3) {
if (self.shouldShowSection3) {
return 44.0f;
}else{
return 0.0f;
}
}else{
return 44.0f;
}
}
Then just define some logic within your IBAction to change these BOOLs around in between tableview's begin/end updates, and the table will show/hide the sections you want.
- (IBAction)toggleSegment:(UISegmentedControl *)sender
{
[self.tableView beginUpdates];
// change boolean conditions for what to show/hide
[self.tableView endUpdates];
}
I have a UITableView that has AllowsMultipleSelectionDuringEditing set to YES. Selecting multiple rows is a big part of my app, and it works great.
The problem is, that on one of my tableView's I don't want the user to be able to select the first 2 rows when in editing mode, so I have implemented the following:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0 || indexPath.row ==1) {
return NO;
}//end
return YES;
}//end
This works how I thought it would, and doesn't show the red checkbox graphic or the option to re-sort the rows. BUT, I can still select those rows that are not editable, and call the indexPathsForSelectedRows method and return the indexPaths of those rows.
How can I prevent the user COMPLETELY from being able to select those rows while in editing mode, and prevent touches on those from being returned when calling indexPathsForSelectedRows? Why isn't canEditRowAtIndexPath: doing this for me?
One way to do this would be to implement – tableView:willSelectRowAtIndexPath: method and check if tableView.editing == YES then return nil for first two cells.
Something like,
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView.editing == YES && indexPath.row < 2 )
return nil;
return indexPath;
}
You can also set the selectionStyle of these two cells as UITableViewCellSelectionStyleNone in editing mode in cellForRowAtIndexPath method.
Did you try the following code?
tableView.allowsSelectionDuringEditing = NO;
or
tableView.allowsMultipleSelectionDuringEditing = NO;
I'm trying to delete or hide a section inside a tableview with static cells on it. I am trying to hide it in function viewDidLoad. Here is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView beginUpdates];
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:YES];
[self.tableView endUpdates];
[self.tableView reloadData];
}
Still the sections appear. I am using storyboards in it. Can you please help me out?Thanks!
I found it most convenient to hide sections by overriding numberOfRowsInSection.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ( section == 1 )
// Hide this section
return 0;
else
return [super tableView:self.tableView numberOfRowsInSection:section];
}
Check the answer found here. How to remove a static cell from UITableView using Storyboards Seems to be a bugger of a problem when using static cells. Hope this helps.
Here you go. This one also removes the vertical space.
NSInteger sectionToRemove = 1;
CGFloat distance = 10.0f; // minimum is 2 since 1 is minimum for header/footer
BOOL removeCondition; // set in viewDidLoad
/**
* You cannot remove sections.
* However, you can set the number of rows in a section to 0,
* this is the closest you can get.
*/
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
if (removeCondition && section == sectionToRemove)
return 0;
else
return [super tableView:self.tableView numberOfRowsInSection:section];
}
/**
* In this case the headers and footers sum up to a longer
* vertical distance. We do not want that.
* Use this workaround to prevent this:
*/
- (CGFloat)tableView:(UITableView *)tableView
heightForFooterInSection:(NSInteger)section {
return removeCondition &&
(section == sectionToRemove - 1 || section == sectionToRemove)
? distance / 2
: distance;
}
- (CGFloat)tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section {
return removeCondition &&
(section == sectionToRemove || section == sectionToRemove + 1)
? distance / 2
: distance;
}
It seems that reloadData makes table view to re-read dataSource. You should also remove data from dataSource before calling reloadData. If you are using array, remove object you want with removeObject: before calling reloadData.
I have a UiTableView and the first cell is only showing information. The following cells are going to push new viewControllers to the window.
I don't want it to be possible to click on the first one, but the second cell (indexpath.row == 1) to be the first cell to click on...
Is there some property I can set to make that first cell "notChooseable"?
try:
if(indexPath.row == 0)
cell.userInteractionEnabled = NO;
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
return;
}
else {
//perform pushing controllers
}
}
I am looking for a way of preventing the deleting of one of my cells. (No delete button should appear next to the cell when the table view is in editing mode.)
How can this be made possible?
Implement editingStyleForRowAtIndexPath and return UITableViewCellEditingStyleNone for that row:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == sss && indexPath.row == rrr)
return UITableViewCellEditingStyleNone;
else
return UITableViewCellEditingStyleDelete;
}
The accepted response works but is not the correct way to do it. There are two methods available: editingStyleForRowAtIndexPath and canEditRowAtIndexPath
editingStyleForRowAtIndexPath: Use when there are multiple different editing styles in the table
canEditRowAtIndexPath: Use when some rows should edit and some should not.
Therefore the correct way to implement your table delegate is:
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == sss && indexPath.row == rrr)
{
return NO;
}
return YES;
}