update title of selected row in Uitableview - iphone

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.

Related

UILabel is not showing values from the UITableview

Sorry guys its me again.
In my project i used UITableview for showing weather.so, I need to show the first row text to UILabel without any button action or watever. I just want cell text to UILabel without any action.
ex:
In the tableview first cell showing text as "Lovely IOS"
I want to show the same text to UILABEL without any action. I took UILabel in the xib.
Thanks in advance
Any sample code please.
you only set action for particular cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 1){
//set action for cell
}
}
In the method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath equate
cell.textLabel.text = [array objectAtIndex:indexPath.row];
LabelName.text= cell.textLabel.text;
You have two options for this either you add add UILable or set the text to he cell.textLabel.text in the following method.
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
You have the data for your cell so what's a big to UIlabel .You just need to set the same text there.
Updated:-
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = cell.textLabel.text;
you will get the text.

Simplest way for UISearchDisplayController to show same cells as the origin

What's the simplest way to make UISearchDisplayController display exactly the same cells (formatting, height, fonts, colors, etc) as the original tableView that it searched on?
or simply put - make it dequeue de same cell identifier?
I am using a the standard subclassed UITableViewController + UISearchDisplayController solution, and prefer to stick to it.
thanks
I actually found the simplest way.
All I had to do is change the default cellForRowAtIndexPath second line of code and add the "self." to tableView to the dequeue cell identifier - this way the the cell style which is in the storyboard always gets dequeued and not the (non-existant) one from the searchResultsController.tableView
Also implement heightForRowAtIndexPath that will return the same height without any check (searchresults table or self.table - we want the same height)
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 62.0f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"PhraseCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// ^^^^ NEW
//...

how could i forbid a tableviewcell to be selected

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;

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.

Tableview cell change cell backgroung image(png) on click

I want to highlit cell when user click on the cell in the function didselectrowatindexpath.?
You have two options:
modify the cell directly in your - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method (but I've had some visual glitch issues with this technique, Problem modifying UITableViewCell accessoryType in tableView:didSelectRowAtIndexPath:):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// do stuff with cell
//
}
set appropriate state in your data model, and call [tableView reloadData], which in turn will result in a call to - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath, where you set up the cell to display according to your data model.