UILabel and UITableViewCell background colours coming out different - objective-c

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.

Related

Static Table View Cell Labels are getting Squished into Header

EDIT
My constraints on custom cells are all weird. Please disregard. Just using standard Left Detail ones now.
I don't understand why in iOS8 now my static table view cell contents are collapsing into the header of each section.
There's really almost nothing in my SettingsTVC.m. This is a just a test with all static data in the storyboard:
- (void)viewDidLoad {
[super viewDidLoad];
}
Have you implemented TableView delegate methods properly i.e
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if(section == CUSTOM_SECTION)
{
return CUSTOM_VALUE;
}
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return height;
}

UITableViewCell like mail on iphone

How can I adjust a UITableViewCell so that it would show two fields after swiping it left - identical to how the mail client does it?
Right now I only see the "Delete" button.
I would like to have the "More" field included there, too...
Also, for the first if clause Cell I do not get a "Insert" field.
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == self.reports.count){
return UITableViewCellEditingStyleInsert;
} else {
return UITableViewCellEditingStyleDelete;
}
}
Is there a way to combine the fields with the | operator or something?
Thanks, EL-
You can use undocumented delegate methods for UITableView in iOS7
- (NSString *)tableView:(UITableView *)tableView titleForSwipeAccessoryButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return #"More";
}
- (void)tableView:(UITableView *)tableView swipeAccessoryButtonPushedForRowAtIndexPath:(NSIndexPath *)indexPath
{
[self setEditing:NO animated:YES];
}
It seems that they are not private and can be submitted to appStore.

Cell text overlaps the editingAccessory when try to delete a row in iOS7

When I compile my app in the new iOS7, I found a problem when entering in the edit mode of an UITableView.
When I press in the red minus button to delete a row of the table, this row indent to the left to let appear the 'Delete' button. However, when this button appears, the text of the cell overlaps the editingAccesory (this happens only when the text is longer than the length of the cell).
How can I remove the overlapping?
Edit: Images in the comments
Edit 2: Tis is the code of the creation of the table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_tweetList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"SessionDetailCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Tweet *tweet = [_tweetList objectAtIndex:indexPath.row];
cell.textLabel.text = tweet.text;
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView beginUpdates];
Tweet *deletedTweet = [_tweetList objectAtIndex:indexPath.row];
[_selectedSession removeTweetsObject:deletedTweet];
[deletedTweet deleteEntity];
_tweetList = [Tweet findAllSortedBy:#"index" ascending:YES withPredicate:[NSPredicate predicateWithFormat:#"session == %#",_selectedSession]];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
[[NSManagedObjectContext defaultContext]saveToPersistentStoreWithCompletion:nil];
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
selectedPath = indexPath;
[self performSegueWithIdentifier:#"EditTweet" sender:self];
}
Solution:
Finally, I put the accessoryButton in the default state, and I use the edit state only to delete the rows. It's the only solution I've found :(
Perhaps, the method "willTransitionToState" can help people to solve similar problems.
You can hide or remove editingAccesory in editing mode , so there is no overlapping there,
set this,
Screenshot:
I came across this question, because in iOS 8.3 I faced the same problem, that it seems not possible to correctly display the editing accessory AND the delete confirmation without the cell content and the accessory item to overlap. Solving this issue without breaking the transition-animations was quite a challenge. ;)
So here is my solution (assuming that you are using autolayout constraints in IB):
Create a UITableViewCell subclass and link it to your table view cell in IB.
Add an outlet from the autolayout constraint specifying the horizontal spacing bewteen the right-most view and the cell's content view (trailing to margin).
Override willTransitionToState and layoutSubviews as shown below.
Table cell subclass:
#IBOutlet weak var horizontalSpaceConstraint: NSLayoutConstraint!
override func willTransitionToState(state: UITableViewCellStateMask) {
if (state & UITableViewCellStateMask.ShowingDeleteConfirmationMask == UITableViewCellStateMask.ShowingDeleteConfirmationMask) {
self.horizontalSpaceConstraint.constant = 49.0; // ugly, delete-confirmation width
}
super.willTransitionToState(state)
}
override func layoutSubviews() {
if (!self.showingDeleteConfirmation) {
self.horizontalSpaceConstraint.constant = 0.0;
}
super.layoutSubviews()
}
The reason why I cannot use didTransitionToState() (and use layoutSubviewsinstead) to reset the layout constraint is, that this function is simply not invoked (as of iOS 8.3) after transitioning from the delete-confirmation-state. It seems Apple did only handle the case, that the user actually deletes the row, but not the case that the delete-confirmation is closed without deleting the row. :(

TableView Background image going over cell textLabel

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]; ?

Objective C: How to add "plus" button in cell

Can anyone advise me on how I can add a "plus" button in a UITableView Cell like what you see in the screen shot below?
You can also put the table view into editing mode, implement editingStyleForRowAtIndexPath: and return UITableViewCellEditingStyleInsert (which puts a green circle with a plus sign).
For example...
- (IBAction)editButtonPressed
{
//toggle editing on/off...
[tableView setEditing:(!tableView.editing) animated:YES];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
return UITableViewCellEditingStyleInsert;
//gives green circle with +
else
return UITableViewCellEditingStyleDelete;
//or UITableViewCellEditingStyleNone
}
When the green button is pressed, the table view will call tableView:commitEditingStyle:forRowAtIndexPath::
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleInsert)
{
//handle insert...
}
else
{
//handle delete...
}
}
The simple way: get an image of the plus, set it for the cell.imageView.image.
You can customize your cell by getting the contentView property of your UITableViewCell and adding the UIImage of the plus button as a subview.
That's not a built-in control. It's probably a custom-style UIButton with that image in its .image property.