Currently my tableview looks like this: as you can see, cell.textLabel and cell.detailTextLable both load this background, and i can't work out how to get them to stop loading the background, and for it only to be the backing on the cell.
Here's my current code to change the cell:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"tableCell.png"]]];
}
How can i get round this problem?
Thanks in advance.
have you tried [[cell contentView] setBackgroundColor:blabla]; ?
Related
I was wondering how one might go about making it so when you tap on a UITableViewCell in a UITableView, a method is called to change the text of a UILabel. Much appreciated!
Assuming you have not tried using didSelectRowAtIndexPath, You could do something like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.label.text = #"Test";
}
I am trying to setup a general colour scheme for my app using code so it's easily modified when porting between projects. However, the two colours on these components keep coming out slightly different colours. I use the following code to setup my UITableViewCell:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath row] %2 != 0)
{
[cell setBackgroundColor:kTableSecondaryCellColor];
}
}
and this for my UILabel:
[self.lTitleBackground setBackgroundColor:kTableSecondaryCellColor];
As you can see I am simply setting the background on both of the elements to kTableSecondaryCellColor which is setup as follows:
#define kTableSecondaryCellColor [UIColor colorWithRed:(158.0f / 256.0f) green:(171.0f / 256.0f) blue:(4.0f / 256.0f) alpha:0.4f]
However, the results of the two colours come out as follows (green colour):
You must use
cell.contentView.backgroundColor = kTableSecondaryCellColor;
You should change background colour for content view of your cell:
cell.contentView.backgroundColor = kTableSecondaryCellColor;
This should fix the issue.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath row] %2 == 0)
{
cell.contentView.backgroundColor = kColorTablePrimary;
}
else
{
cell.contentView.backgroundColor = kColorTableSecondary;
}
}
This is the code I am currently using, the opacity is only being detected on iPhone but I have a breakpoint in this code and it's definitely being called when I use the iPad just the opacity isn't showing. I've been through the setup and the cells are both setup identically, the same with the UITableViews.
I have following issue: I have a UITableView.If you click on a cell, a button is added on cell. No Problem till now... but: After adding button i want the TableView (after its reload) to select the just edited (or added) TableCell.
I've tried this in cellForRowAtIndexPath method
if (bool) {
[cell setSelected:YES animated:YES];
}
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor colorWithRed:34/255.0 green:139/255.0 blue:34/255.0 alpha:1.0]];
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];
return cell;
But it didn't work.
Any suggestion or sample code would be appreciated. Thanks .
For the standard selection you use the following method, described at UITableView reference docs
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
Your design might be improved if you use the custom cell storing information of selection count, that is much better than reloading the table and adding a lot of cell logic outside of the cell.
Why are you using
[tableView reloadData];
You can access the selected cell properties and controls without reloading the data using:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
Here is some code that can help you:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.myButton setHidden:false];
}
I ran into some strange behaviour after I selected a cell using this code :
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
CustomCell * cell = (CustomCell*)[_myTableView cellForRowAtIndexPath:indexPath];
[cell setSelected:YES animated:NO];
After this i couldn't select exactly this cell it just didn't respond and
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
was never called.
This wasn't called either:
-(void)tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
What i am doing wrong guys?
You need to call both methods manually because manually cell selection does not call delegate method.
[cell setSelected:YES animated:NO];
[self tableView:_myTableView didSelectRowAtIndexPath:indexPath];
According to Apple's documentation it is expected behavior that the methods you mention are never called after programmatically selecting a cell. More info is in this post.
I have a table, and I noticed that when I do the "swipe to delete", the red button is animated only the first time, the return (if I do another swipe) disappears without animation. These are the methods I use for this step:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
...
}
- (void)tableView:(UITableView *)tableView
didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView reloadData];
}
I forgot something to animate the return?
Thanks a lot!
Yeah, leave out the [tableView reloadData];. You only need to reload the table in -tableView:commitEditingStyle:etc:, and arguably not even then—it’s better to use the UITableView method -deleteRowsAtIndexPaths:withRowAnimation:. Reloading it is unnecessary and is why your delete button is disappearing instead of animating out.