I've got a subclass of UITableViewCell. I want to create a shadow on the labels dependent on the brightness of the image on the background. However, this image is set in the TableViewController. When awakeFromNib is called, self.backImage.iamge is nil. I tried implementing initWithStyle in the subclass itself but it logged nil as well.
When the cell is eventually loaded tho, the image is displayed.
Is there an event that happends when the cell is updated trough
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
?
So your table view controller is setting the background image for the cell? I assume it does this in cellForRowAtIndexPath after dequeueing the cell?
If so, try creating a method on your custom cell that sets the label shadow. Call the method after dequeueing the cell and setting the background image. If necessary, pass a parameter in the method that tells how dark to make the shadow.
Related
What is the difference between setHighlighted and setSelected in UITableViewCell ?
If I just want to not highlight a cell when a selection is made, Should I override setHighlighter or Just set the selectionStyle to NONE.
setHighlighted will mark the object with a highlighted colour (or glow, depending on your settings) when the finger is touching down. On touch up, the highlight disappears and the object state returns to normal.
setSelected on the other hand, while similar, will be set on touch down, and will remain in the highlighted state until the next touch down event occurs.
What I think you want is to override setHighlighted (just return inside the method and don't call super), but more simply, you can just set the cell's UITableViewCellSelectionStyle to UITableViewCellSelectionStyleNone.
To not let the UITableView highlight the cell, implement and return NO in the tableView delegate method - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath.
I have TableViewController and ViewController. In 3 cells change pictures depending on the index and intForString in ViewController.
In the first session I work with all cells. In the second I work with one cell. When I go to the third session image stay only to the cell in which I worked the second session. And the other two images disappear. How to make that all the images were displayed?
I don't understand what you are asking when you say "How to make that all the images were displayed?"
However, this bit is very wrong:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell1 = [tableView cellForRowAtIndexPath:indexPath];
The table view data source method tableView:cellForRowAtIndexPath: is supposed to configure and return a new (or recycled) table view cell.
The method you're calling in the first line is the table view instance method cellForRowAtIndexPath. That method only returns a cell if there is a cell on-screen for the specified indexPath.
That won't work.
You want to use code that calls dequeueReusableCellWithIdentifier:forIndexPath. If you've defined a class for your identifier, that method will always return a valid cell. If not, you need to add code that checks for nil and creates a new cell using the UITableViewCell method initWithStyle:reuseIdentifier:
I have an UIImageView set as the background to a custom cell (prototype cell) .How can I change the image in the image view , when the cell is selected?
If you want to change the image of the imageview on cell selection then you will need to do it in delegate method of tableview here:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}
If you have a custom cell and you have set its background as an image view then you must also create its property in custom cell's class and change the image of the image view through that property in the mentioned delegate.
Did you try the property highlightedImage ?
I've subclassed UITableViewCell to display a UIImage and two UILabel subviews. In the view controller for the table view, in the method cellForRowAtIndexPath: I've enabled an accessory view via setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton.
Cells display correctly.
When I tap on the accessory disclosure button I want to replace the two label subviews with two different label subviews. My approach was the following:
In the subclassed UITableViewCell, inside layoutSubviews, create the
CGRects for the "alternate" labels, position them in the same
places as the two "original" label and hide them via setAlpha:;
When the disclosure button is tapped swap out the set of two
label by adjusting their respective alphas.
The problem is I can't figure out what logic in layoutSubviews I'd use to know whether the accessory button has been tapped. I see that in the view controller accessoryButtonTappedForRowWithIndexPath: is called when that button is tapped and from there it seems like I would call layoutSubviews but can't figure out how to use that fact to accomplish what I'm trying to do.
Am I going about this all wrong? Instead of hiding/showing CGRects with alpha should I simply be creating another subclass of UITableViewCell?
When I tap on the accessory disclosure button I want to replace the two UILabel subviews with two different UILabel subviews.
I'll do the following. Create a public method in your UITableViewCell subclass like the following:
- (void)changeContentForCell;
In this method you could set the contentView as you prefer.
Then in your view controller implement
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cCell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];
[cCell changeContentForCell];
}
This is a simple example for change the content. In my opinion you don't have to use layoutSubviews to add views.
Leave this logic in changeContentForCell and then call setNeedsLayout to change your layout. Maybe you could have a variable that tracks the state for your cell: normal state or modified state.
Hope it helps.
i am using custom UitableViewcell (MainTablecell)to display the records. i am also providing edit and delete functionality.so that i want to shrink my custom table cell, can any one suggest me how to do this.my custom cell looking like bellow
cell.shouldIndentWhileEditing = YES;
and
-(BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
i tried with above poece of code but
Both are not working for me,How can i shrink all lables in my cell could you please help for me
When your tableView goes into edit mode, resize the labels.
shouldIndentWhileEditing: only makes the left side of the cell indent and does nothing for the content. You will need to manually resize the labels.
Depending on the design and code of that custom cell, you can call the label and perform setFrame: when in editing mode, just detect that mode and reload your tableView.