Since upgraded XCode to Version 3.2.3 with iPhone SDK 4 my code doesn't work anymore.
I have a default cell with style UITableViewCellStyleSubtitle and want to set the textAlignment of textLabel and detailTextLabel to center, but nothing happens.
Same code used before now not working anymore. On UITableViewCellStyleDefault center alignment still works.
Does anyone know how to solve this? I don't want to use a custom cell only in fact of this.
The reason why text isn't centered is because the label is only as wide as the text. You can confirm this by setting the background color of the text label:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.textLabel.backgroundColor = [UIColor orangeColor];
}
Setting the cell width manually also doesn't seem to have an effect. So you should really add your own subviews or create your own subclass of UITableViewCell.
The docs for UITableViewCellStyleSubtitle even say:
A style for a cell with a left-aligned
label across the top and a
left-aligned label below it in smaller
gray text. The iPod application uses
cells in this style.
here is a two-part solution to alter the label width after rendering, thus allowing the centering to happen:
-- (void) updateCenteringForTextLabelInCell:(UITableViewCell*)cell {
UILabel* label = cell.textLabel;
label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, cell.contentView.frame.size.width, label.frame.size.height);
}
-- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// do your usual
// call the helper method
[self performSelector:#selector(updateCenteringForTextLabelInCell:) withObject:cell afterDelay:0.05];
}
ok finally the Apple development team answered my bug report from 23rd June 2010:
"Please try setting the text alignment
in layoutSubviews after calling
super."
Related
Following is the image i have taken from an app from the App Store:
I have created an app that is using StoryBoard with custom tableview cell. The data is loaded in it from a web service. This is what i want:
1) How can i change the size of the uitableviewcell according to the image? For example if one image is 640*500, then the UITableViewCell will change its size accordingly. If the size if the image is 640*1000, then the cell should change its size accordingly.
2) How can i add the grey coloured border around the tableview cells just like in the image above?
3) How can i add a shadow dropping from the UITableViewCell just like in the image above?
Thanks!
Currently i have a partial answer to your question. Part 2 of the question that is the colored border can be solved by the following code:
[cell.contentView.layer setBorderColor:[UIColor colorWithRed:222/255.0f green:222/255.0f blue:222/255.0f alpha:1.0f].CGColor];
[cell.contentView.layer setBorderWidth:5.0f];
Part-1
The height of the tableviewcell is determined using
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
Get the image size
CGFloat width = myImage.size.width;
CGFloat height = myImage.size.height;
use this to return the height of your cell.
*/
}
Part-2 and Part-3
Use..
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
//This will give you access to the cell being rendered and any customization you want can be done here... You can use the earlier answer for this part.. I havent tried it but should work..
}
Hope this helps..
I'm managed to make my UITableView rows / cells black when data is loaded, to a fashion, I get a white line in between rows, which makes it look awful. How can i make them fully black with and without data ?
Heres the code I'm using at the moment
- (void)tableView:(UITableView *)tableView willDisplayCell:
(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor blackColor];
}
Heres what I get (with white lines remaining) :(
Change the separator style of your table view.
yourTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
You can do it in Interface Builder too on the properties palette.
-- Edit for clarity following comments below --
Set the entire tableView to have a black background:
[yourTableView setBackgroundColor:[UIColor blackColor]];
This can also be done in the Interface Builder properties panel.
I'm loading a custom nib file to customize the cells of a UITableView. The custom nib has a UILabel that is referenced from the main view by tag. I would like to know if it is possible to change the shadow color of the UILabel when the cell is selected to a different color so it doesn't look like in the screenshot.
I prefer to make the shadow color change inside the TableCell code to not pollute the delegate. You can override this method to handle it:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animate
{
UIColor * newShadow = highlighted ? [UIColor clearColor] : [UIColor whiteColor];
nameLabel.shadowColor = newShadow;
[super setHighlighted:highlighted animated:animate];
}
You could change the label's shadow color in -tableView:willSelectRowAtIndexPath: in the delegate. For instance:
-(NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.shadowColor = [UIColor greenColor];
return indexPath;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.shadowColor = [UIColor redColor];
}
I had the same issue and none of the above solutions quite worked for me - I didn't want to subclass UITableViewCell and also had some tricky selected/highlighted state changes done programmatically, which did not play well with the solutions above.
MySolution:
What I did in the end is to use a second UILabel underneath the primary UILabel to act as a shadow. For that 'shadow' UILabel you can set the 'Highlighted Color' to 'Clear Color'.
Obviously you have to update the shadow label each time you update the primary label. Not a big price to pay in many cases.
Hope that helps!
The simple answer, at least for the example shown above, is to not display the shadow in the first place. Since you can't see the white-on-white anyway, set the shadowColor to -clearColor.
If you actually need a shadow though, overriding the -setHighlighted method is the best solution. It keeps the code with the cell, which I think is better than trying to handle it from the table view.
I am building a UITableViewCell. I am using the style UITableViewCellStyleSubtitle for my table view cell. In the textLabel of the cell I have data that can require a wrap. So to accomodate, I want to move down the detailTextLabel so that the wrapped text isn't covered by the text in the detailTextLabel. I am attempting the following:
CGPoint tempPoint = tableViewCell.detailTextLabel.center;
tempPoint.y += 100;
tableViewCell.detailTextLabel.center = tempPoint;
I have tried similar approaches with the frame of the label, but I could not get it to move. When I log the y of the center point before and after, I always see it start as 0 and then it is just 100 afterwards. I am using the %f flag in NSLog to view the value. This all occurs in the function:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Any help would be great.
I subclassed UITableViewCell and implemented the - (void)layoutSubviews method. I moved the label in that method.
As a follow up to: UILabel subview in UITableViewCell isn't showing up. (iPhone Dev)
I have found out that the UILabel I added as a subview is showing up and positioned correctly, however when I set the cell's labelField, it seems to "overlap" the label I have added in the subview.
In other words "bar" only shows up when I comment out the first line of the following code snippet:
cell.textLabel.text = #"foo"
// sub view text label
UILabel *valueField = (UILabel *)[cell viewWithTag:111];
valueField.text = #"bar";
Is there a way to make the cell.textLabel not overlap the UILabel that's on the "same line"? maybe by adjusting the underlying frame of cell.textLabel?
This problem also seems to only occur when I'm compiling with an SDK > 2.2.1, did something change that would affect this in 3.0?
Thanks
UITableViewCell´s textLabel has a non clear background. I think that the position from your custom label to this one is so tiny that when the default textLabel shows text, it´s background covers your custom label (bar).
Change the background color for the default textLabel field in this message:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}