I have created a custom UITableViewCell with a label on the far left (respects the parent margins) and it displays correctly. However, when I set an image using imageView.image (from the UITableViewCell), the label does not move to the right and so the label and the image are on top of each other. Any ideas how to make the label behave like the default label where it will move to the right to make way for the image?
If you create the cell from storyboard make sure you have set the UITableViewCell style to Custom.
Edit:
I saw your comment above.
If you don't wish to create a custom UITableViewCell class, you can still use default UITableViewCell with style set to Basic
So you can obtain its properties:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = #"Your text";
cell.imageView.image = [UIImage imageNamed:#"Your image name"];
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame: CGRectZero];
cell.accessoryView = mySwitch;
// Define the state of the switch because the cell will get recycled as you scroll.
BOOL isOn;
[mySwitch setOn:isOn];
return cell;
}
The other solution is mentioned as commented above: You have to subclass UITableViewCell, drag and drop 3 IBOutlets and reference them in .h file.
Related
I have a UIViewController that contains a UITableView. In my table view I have a prototype cell in which I want to place custom labels. I cannot get any of my custom labels to render in the cell. The datasource and tableview delegates are connected and functioning properly. For my first attempt, I dragged a UILabel control onto my prototype cell. I set its tag to 1000. Here is my code to populate the cells:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
UILabel *customLbl = (UILabel*)[cell viewWithTag:1000];
if(indexPath.section == 0){
[cell.textLabel setText:[rData.currentHeightReadings objectForKey:[heightKeys objectAtIndex:indexPath.row]]];
[customLbl setText:#"CFS"];
} else {
[cell.textLabel setText:[rData.currentFlowReadings objectForKey:[flowKeys objectAtIndex:indexPath.row]]];
[customLbl setText:#"Ft."];
}
return cell;
}
The default textLabel for each cell renders as it should. However, the custom label does not appear. I verified that the prototype cell was actually rendering by changing the background color of the cell's content view. In my story board I have set the prototype cell's reuse identifier to match my code. The style is set to 'Custom.' What else should I be doing? Thanks!
Verify that your autolayout constraints are correct such that the UILabel is really positioned where you expect it.
I had the same issue once and I've solved it by not calling any methods to cell.textLabel property. Just don't set its text in any way.
Try it out and report back.
Try this:
UILabel *customLbl = (UILabel*)[cell.contentView viewWithTag:1000];
I have a custom cell for my UITableView ; that custom cell comes from a class that extends UITableViewCell.
There is only a .xib where i crated the actual cell, and the links to these items in the .h.
I'm not using the .m, it's only the autogenerated awakeFromNib and setSelected:selected:animated in there.
When i create my cell in the cellForRow method, it's all fine, it appears correctly on the screen.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"CustomCellTableViewCell";
CustomCellTableViewCell *cell = [self.tbUpcomingEvents dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
tmp = [_eventList objectAtIndex:indexPath.row];
cell.lbDescription.text = tmp.description;
cell.lbTitle.text = tmp.title;
cell.imageView.layer.cornerRadius = 20;
cell.imageView.clipsToBounds = YES;
cell.layer.masksToBounds = YES;
cell.imageView.image = [UIImage imageNamed:tmp.type.typeImage];
cell.lbTimeStart.text = [[[timeFormatter stringFromDate:tmp.startTime] stringByAppendingString:#" - "]stringByAppendingString:[dateFormatter stringFromDate:tmp.startDate]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
My issue is the following : when I start scrolling the tableview, the NEW cells are messed up, specifically the image. It simply gets bigger/wider and (obviously) deformed. I don't really know what caused it or how to fix it.
The cell is associated with the reuse identifier from the cellForRow method and the custom class in the .xib file. The UITableView has nothing in particular (and has no other link than the storyboard -> .h, delegate, datasource)
Any clue?
I found my issue. In this specific case, my imageView in my customCell.h was called "imageView". Which is a reserved named and, i guess, created conflicts at (some?) points in the creation of the cells.
I don't know the why's, but i know what fixed the issue. Now i've simply put another property name for my image and i'm golden ;)
Try to replace your code:
cell.lbDescription.text = tmp.description;
cell.lbTitle.text = tmp.title;
cell.imageView.image = [UIImage imageNamed:tmp.type.typeImage];
cell.lbTimeStart.text = [[[timeFormatter stringFromDate:tmp.startTime] stringByAppendingString:#" - "]stringByAppendingString:[dateFormatter stringFromDate:tmp.startDate]];
from cellForRowAtIndexPath to willDisplayCell method.
The idea is to make UITableViewCells templates in tableView:cellForRowAtIndexPath:, but to fill cells with particular values in tableView:willDisplayCellForRowAtIndexPath:.
uncheck autoresize subviews in interface builder
I have a dynamic TableView that looks like this.
However, I'd like to position the text more neatly, like this.
Here's the code I'm currently using:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =#"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell.textLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:17.0f]];
cell.textLabel.text=[[self.responseArray objectAtIndex:indexPath.row]objectForKey:#"name"];
return cell;
}
I've experimented with putting a label into the TableViewCell that's positioned where I want the dynamic text to be, but I've been unsuccessful in calling it in my code. How can I do this?
You should create a custom cell subclass. This subclass should have an #property in the .h file which makes your custom label available. In the .m (or XIB) you create (or connect) the label to the property (outlet).
Now, instead of using cell.textLabel you use cell.customTextLabel. Just remember to register the custom cell class against your CellIdentifier.
You can also configure the font and size on the label in the cell .m (or XIB) so you don't need to do it in code.
You will also want to change to:
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
so that the compiler knows that class the cell is (and thus what properties it has).
There is no need to create subclass of UITableViewCell. Coz, you want just change origin of your text.
I recommend to add a UILabel to your cell as subview.
cell.textLabel.text = #"";
UILabel *textLabel = [[UILabel alloc] initWithFrame: CGRectMake(10,10,300,20)];
textLabel.text = #"Text here";
[cell.contentView addSubview:textLabel];
2-3 lines only and you needn't create a subclass.
I'm a beginner in iOS development and I have to put in the same UITableViewCell a label (on the left), a subtitle (on the right) and a slider in the middle.
For now the Label and subtitle are in a cell above the slider.
Is there any way to do that ?
Thanks in advance.
Yes are you using StoryBoards? if so change the cell Style to Custom then drag the other elements into the cell place them were ever you want.
Make sure you set the cell identifier name
May sure you set the tag value to get to them later in the cellForRowAtIndexPath
Example:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:"stateCell" forIndexPath:indexPath];
UILabel *stateLabel = (UILabel *)[cell viewWithTag:1000];
StateProvince *myState = [self.states objectAtIndex:indexPath.row];
stateLabel.text = myState.label;
}
When I push a UIViewController onto my UINavigation controller like:
[(UINavigationController *)self.parentViewController pushViewController:[[[Fonts alloc] initWithNibName:#"Fonts" bundle:nil] autorelease] animated:YES];
Where Fonts.xib is a UIView with only UITableView controlled by a Fonts object that is a subclass of UIViewController and acts as the UITableView's dataSource and delegate.
In the Fonts object I create a UITableViewCell like:
- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: #"BlahTableViewCell"];
if (!cell) {
cell = [[UITableViewCell alloc]
initWithStyle: UITableViewCellStyleDefault
reuseIdentifier: #"BlahTableViewCell"];
[cell autorelease]; // Delete for ARC
}
return cell;
}
And then I change the font of the cell here:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell.textLabel setFont:[(UIFont *)[self.listOfFonts objectAtIndex:indexPath.row] fontWithSize:cell.textLabel.font.pointSize]];
cell.textLabel.text = [(UIFont *)[self.listOfFonts objectAtIndex:indexPath.row] fontName];
}
listOfFonts is an NSArray of UIFont objects.
When the view appears it looks like UITableView without changed fonts
If I call reloadData on the UITableView or if I drag the UITableViewCells off screen with my finger and let them bounce back they are redrawn and the view the cells display with the labels having their fonts changed.
It seems like the issue is the UITableViewCells are being drawn too early. If I delay the drawing of them everything looks correct but I want the UITableView to be displaying correctly when the UINavigationController slides my view into place.
Any idea what I am doing wrong?
EDIT: I uploaded a simple and straightforward example of my issue to Dropbox. http://dl.dropbox.com/u/5535847/UITableViewIssue.zip
SOLVED IT!
Ok so I was having exactly the same issues as the original poster and this was the problem.
The line that's causing issues is:
[cell.textLabel setFont:[(UIFont *)[self.listOfFonts objectAtIndex:indexPath.row] fontWithSize:cell.textLabel.font.pointSize]];
Specifically, your issue is because you're trying to feed the cell's textLabel its own pointSize, but pointSize doesn't exist yet so strange bugs occur instead. For me, I noticed that a "transform" was failing due to a singular matrix being non-invertible. As soon as I hardcoded a standard value as my pointSize I saw all my labels draw with the proper font instantly. Note: this makes sense as to why a redraw worked, because then your textLabel does indeed have a pointSize.
In any case, you need to explicitly set your pointSize here, no using what the textLabel "already has" because it doesn't have anything until you're "reloading" a cell.
Set the label font inside -tableView:cellForRowAtIndexPath:.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
// do it here if your font doesn't change ....
}
// otherwise here with your font ...
cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
return cell;
}
I'm not sure that table cells are designed to be customisable in this way. The table cell may assume that you won't customise the font, and so not draw itself in a way that's compatible with what you are trying to do.
You'd be better off creating a custom table cell, or appending a UILabel as a subview to the table cell when you create it, and them setting the font of that label instead.
It may seem like overkill for such a small customisation, but it's flexible and it's guaranteed to work.