How to update UITableViewCell drawing - iphone

My app displays some news in a UITableView. For each news-topic, the background color of the UITableViewCell is different (added a UIView in my custom cell).
So far, it works. But If I press on a UIBarButton to deselect one news-category, the content changes, but the background stays the same as before.
The color does not change, because the cell does not reload again after button click.
Any ideas?
Thank you!

You can set the color for the cell in (as per your logic):
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
This will make sure the color is set every time the cell is displayed.

In your cellForRowAtIndexPath method, make sure that you're setting the color of the UITableViewCell.
Also, use [tableView reloadData];

Related

How to make table cell separator to our desire view

Hi i want to set a table cell seperator to the following fashion is there any coding to
make this manually
You can't.
You can, however, hide the cell separator and add the view you want at the bottom of each cell.
You can achieve this by using background image of cell. Make a image for cell with footer black line and hide the cell separator so it will look like above image.
You can set the cell separator style to none and add an image to the custom cell.
set the section footer/header view as your desired separater view and set cell separater as none
in the following delegate method,
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
[cell setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[cell addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImg.png"]]];
}

Background image does not extend to accessory view background in table view cell

I'm using the code below to set a background under each cell in my table view. It works fine but there's a white space under the accessory view on the right side so the background does not cover this area. Any idea on how to fix this issue ?
(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"cell-background.png"]];
}
Thx for helping,
Stephane
You probably want to create a custom cell. Please see Apple's documentation about contentView.
To add subviews to a cell to customize it furthur please see this. It also has a section on how to create custom cells from XIBs.

Delete Button in UITableView

i want a Delete Button in my UITableView like in die AdressBook when editing a contact.
Is this a custom cell, or a UIButton?
To make such button you can do following:
Make your UITableView style Grouped.
Add +1 section to your table.
Say to your table that it is one cell for last section
Just return custom cell (with red background and appropriate label) for last section
If you implement the Delegate method's for edit the tableview
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
And than implement the DataSource method
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
For allowing to get that delete button when you swipe a cell use the
Cell Editing Style : UITableViewCellEditingStyleDelete
// Edit: Wrong answer.
Do you mean the Big one at the bottom or with the sign on the left?
use....
firstly use
[btn removeFromSuperview];
and
[tblobj reload];

Add a view to a UITableViewCell's contentView when it is selected

I am trying to add a new view with 5 buttons to a cell once it is selected. I am trying to do this in 3 different ways. The first one is by checking if it is selected inside the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method.
if (cell == nil ) {
//initial customizaton of cells
} else {
if ([cell isSelected]) {
//I would add the view to the cell's contentView here but it never enters this if even though the cell is still blue, I scrolled the table back and forth several times and the blue cell never evaluated here
}
}
I have also tried to do it inside the following two methods:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
And:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
I used tableView:cellForRowAtIndexPath: to get a reference to the cell and when I had that reference I tried adding the view inside the cell's contentView but it didn't seem to work(by "didn't work" I mean it didn't appear, I'll debug this further).
I know I didn't provide much detailed code but that's because I mainly want to know what's the general principle of how it should be done. I want to know why the cell doesn't stay with the selected property equal to YES even though it stays blue (when selectionStyle is the default one) when I tap it. I want it to stay "blue"(selected) with that view inside it until I deselect it and remove said view.
Did you try creating your own custom UITableViewCell? I think that is the point, you can overload setSelected: method there.
I followed NR4TR's advice and so far so good. I have subclassed UITableViewCell and have overridden the setSelected method in it.
Hoping this is useful to someone else in the future!

Update UIViewTableCell's background when accessory URL is clicked

In the tableView:accessoryButtonTappedForRowWithIndexPath: selector, I want to update the backgroundColor of the cell.
I can get the UIViewTableCell using cellForRowAtIndexPath: but I'm not sure how I can update its background color. The code beliw doesn't seem to work.
[_tableView beginUpdates];
cell.backgroundColor = [UIColor grayColor];
[_tableView endUpdates];
Any suggestions?
I would first try [cell setNeedsDisplay];.
Also beginUpdates and endUpdates is meant to be used when you are going to be inserted/deleting rows etc. so I don't think it's necessary here.
Background colors on cells is a tricky thing because the table view will do some magic behind the scenes to handle cell selection. To get reliable behaviour you need to set the cell's background color in this UITableViewDelegate method:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out.