how could i forbid a tableviewcell to be selected - iphone

As usual, when a tableviewcell is touched, it will be selected. But now i need one cell in my tableview never be selected.how could i forbid a tableviewcell to be selected?
After modifying didSelectRowForIndexPath: method. It works now. Thx ALl:)
Once the cell (need to be forbidden) is selected i just use the selectRowAtIndexPath method to select the former selected cell to be selected again.

The correct way is to use tableView:willSelectRowAtIndexPath:.
Unlike all the other answers this will work with segues too ;-)
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath should not be selected) {
// if another indexPath should be selected instead return this indexPath
return nil;
}
return indexPath;
}

In your didSelectRowForIndexPath: method, you can check if the cell meets your quality standards and accordingly, show a UIAlertView or go ahead if it meets the requirements

You can disable the user Interaction for that very cell or set the selection Style None for that cell. Hope that helps!

In your -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method add the following line to the cell you want to be not selectable:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Hope it helps
EDIT
And in your -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method add the following at the beginning:
if (indexPath == indexPathOfTheCellThatShouldntBeSelected) return;

Related

Need to enable UITextField in row of UITableView when cell is selected in iOS

I have created a custom cell that places a UITextField in each row of a UITableView. What I would like to do is enable the UITextField for a particular row (i.e. activate the cursor in that textfield), when the user presses on anywhere inside the cell.
I have the following method where I try to do this:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[_cell.textField setUserInteractionEnabled:TRUE];
}
However, this is not working. Can anyone see what I'm doing wrong?
You're on the right track placing this inside didSelectRowAtIndexPath:. However, setting user interaction enabled on the textfield just means that the user would be allowed to interact with the object if they tried. You're looking for becomeFirstResponder.
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell.textField becomeFirstResponder];
}
Try this code:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[_cell.textField becomeFirstResponder];
}
What is this _cell iVar? Are you setting it somewhere?
I think what you are trying to accomplish is this:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.textField becomeFirstResponder];
}
Notice that in this way you are getting the cell for the tapped indexPath.
And also, just for information, it's a pattern in objective-C to use booleans as YES or NO, instead of true or false.

update title of selected row in Uitableview

i'm using the following code to get the selected cell in the UiTableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// Mofify the cell here
}
when i change the cell text or color it does not make any change in the actual UitableView
I would suggest that you reload the cell. Use:
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:NO];
You should keep track of which cell was selected, and update the cell in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I don't know why the text isn't updating for you when you change it in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
but even if you get it to work, it will revert if you scroll that cell off the screen.
UITableView is a subclass of UIView and UIView has this method to force it's refresh
- (void)setNeedsDisplay
But if you don't take that change into account in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
you may never see the change.
UITableViewCell has the -titleLabel method which returns a UILabel. Then you might use the setText: and -setColor: methods.
Try doing:
Set the backgroundColor property of the UITableViewCell.
Set the backgroundColor property of the UILabel (may not do what you need).
Set the backgroundView property if the UITableViewCell to a custom class you make by subclassing UIView, where you create a label and color.
Also, you can't just change the cell color, you need to have a data source from which cellForRowAtIndexPath: reads, which sets the cell color there. This way, the color change will persist.

UITableViewCell with 2 accessory types

I would like to make a UITableViewCell with one label and two accessory types:
Unselected cells should display a UITableViewCellAccessoryDetailDisclosureButton accessory.
The selected cell should display both the UITableViewCellAccessoryDisclosureIndicator and the UITableViewCellAccessoryDetailDisclosureButton accessories.
The only way I know how to do this is by using an image for the selected cell's accessory view. Is there an alternative way to do this?
Make a custom UITableViewCell (numerous tutorials and examples online and also in the documentation).
In your
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedIndex = indexPath //selectedIndex is a property
}
Then in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//usual cell stuff
if(indexPath == selectedIndex)
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
else
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
}
So the trick is just to keep a reference to the selected cell and set the indicator accordingly.
Note that you might want to test if the cell is already selected before setting the selectedIndex, in that case you should set selectedIndex = nil.

How to make UITableViewCell highlighted state persist

I have a UITableviewCell. When a user clicks the cell, Im saving the indexpath and then calling the cellforrowAtIndexpath method to get the cell and then call the SetHighlighted:TRUE on that cell.
This works fine but the problem is when I scroll up and down the tableview, the selected cell when reappears, is not highlighted. How do I make the highlighted blue color persist so the user can visually see their selection even after scrolling the table up or down?
Thanks
save the indexpath of the selected cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedIndexPath = indexPath;
}
and compare in tableVIew:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// configure cell
if ([indexPath isEqual:self.selectedIndexPath]) {
[cell setHighlighted:YES];
}
else {
[cell setHighlighted:NO];
}
return cell;
}
However, keep in mind that apple discourages the use of the cell highlight state to indicate selected cell. You should probably use cell.accessoryType = UITableViewCellAccessoryCheckmark;

Checkbox cell in a table view: User can't check it

I need help in using checkbox cell. I currently added the object to tableview. It looks ok until i tried building and running the program where I cannot check the checkbox. I am currently using a tableview which displays items runtime with a checkbox for each item so i can have multiple selections.
I am new to xcode and I have been stuck for a week with this problem. i tried google but still no luck.
Any snippets, answers, or explanations is very much appreciated.
First we need to edit this method: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath. Assuming you generated a Navigation-based application, this method should already be there, only commented out. I don't know the exact details of your implementation, but you somehow have to keep track of the checkbox state for each cell in the tableView. For example, if you had a BOOL array, the following code would work:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (checkboxArray[indexPath.row])
checkboxArray[indexPath.row] = NO;
else
checkboxArray[indexPath.row] = YES;
[self.tableView reloadData];
}
Now that we know what cells need to have a checkmark next to them, the next step is to modify how the cell is displayed. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath handles the drawing of each cell. Building off the previous example, this is how you would display the checkbox:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (checkboxArray[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
cell.accessoryType = UITableViewCellAccessoryNone;
// Configure the cell.
return cell;
}
If we don't call reloadData, the checkmark will not show up until it goes off-screen and reappears. You need to explicitly set the accessoryType each time because of the way cells are reused. If you set the style only when a cell is checked, other cells that may not necessarily be checked will have a checkmark when you go to scroll. Hopefully this gives you a general idea on how to use checkmarks.