Modify outlet inside UITableView custom cell - uitableview

I have a custom UITableViewCell that has 2 UITextFields and a UIButton. The text fields are set in the cell:ForRow:AtIndexPath: method. What I need is to change the opacity of this UIButton dynamically, without having to reload the entire tableview. I was thinking of something like cellForRowAtIndexPath:, but how to access the outlet in this cell?
Thanks

When you create the cell in cellForRowAtIndexPath: just maintain a pointer to it in code. If that button is on every cell then you can just create an array of buttons so that you can easily access the button for the cell you want.
UIButton *cellButton = [[UIButton alloc] init];
[allCellButtons addObject:cellButton];

Related

How to distinguish two UITableView having a same custom cell

Hi I have two UITableView in a UIView class and are loaded with same custom UITableViewCell. Custom tableview cell contains a UITextField. That means the two UITableView contains a UITextfield of same custom cell. When I select any of that textfield, how do I know which tableView's textfield is selected? Please help me..
UITextField *txt = ----;
txt.superView.superview will give you the required UITableView instance.
To be more clear :
UITableViewCell *cell = txt.superView; // In your case custom cell
UITableView *yourTable = cell.superView;

UIButtons in UITableViewCell do not show textLabel changes until cells scroll off screen

I have a UIViewController with a calendar and below it is a UITableView with a UITableViewCell that has a series of UIButtons that I change the button text depending on the data that I retrieve. When a date is selected I update a NSArray and call [self.tableView reloadData]. The first row populates as it should, the UIButton titles show the correct data. The rest of the rows show the default values for the UIButtons from the storyboard. If I scroll a cell off the screen and let it come back it displays the correct data (i.e. updates the titles of the UIButtons). I'm not sure why this is happening. I tried adding
[cell setNeedsLayout] before the cell is returned but it has not helped.
Are you modifying the testLabels directly? UIButtons have iffy behavior sometimes if you do. Try using the UIButton methods to change the textLabel's properties, such as setTitle: forState:

UIButton not working in Custom UITableViewCell

I have created XIB for custom UITableViewCell. And inflated this in cellForRowAtIndexPath. I used cell accessoryType and UIButton above accessoryType view. So the button click event is not working. If I remove the accessoryType, it is working fine.
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
I used for button focus,
[cell bringSubviewToFront:cell.aButton];
But this also not working.
What will I do to use accessoryType and UIButton in Custom UITableViewCell?
Thanks in Advance.
You need to add the button into the contentView of the cell. You should be able to do this in interface builder or in code by
[cell.contentView addSubview: aButton];
The contentView of a tableViewCell is where objects like UIButtons and UIImageViews are placed.

Detecting taps on Custom Cells of UITableView

So i have a custom UITableViewCell which contains three UILabel. The UITableViewCell is such that the UILabel completely cover the cell.
Now i want to detect whenever user taps on the cell. Problem is as the UILabel cover the cell I cannot use UITableView delegate method for detecting touch on the cell.
I thoughout about using gesture recoginzers on the UILabel but then i don't get the index of the touched cell. I also thought about placing a transparent button on top of the cells but here there is also the same problem that i can't get the index of the touched cell.
Can anybody guide me with an approach on how can i accomplish detecting taps with tableView didSelectRowAtIndexPath when UILabel are covering the cell.
Try to set userInteractionEnabled to false in all labels in cell. This will pass touch to the cell and then you can use UITableView delegates. Be sure that cell stays userInteractionEnabled = YES
To get the touch event besides didSelect method try this. In your custom cell class add a block like,
typedef void(^TappedCell)(CustomCell *cell);
and add a property for it,
#property (nonatomic, strong) TappedCell tappedCell;
and on transparent button action
- (IBAction)buttonTapped {
if (self.tappedCell) {
self.tappedCell(self);
}
}
And in cellForRow method
__weak type(self)weakSelf = self;
cell.tappedCell = ^ (CustomCell *tappedCell) {
//Ur actions goes here
//for getting index
NSIndexPath *indexPath = [weakSelf.tableView indexPathForCell:tappedCell];
[weakSelf someActionWithIndexPath:indexPath];
};
It doesnt matter whether UILabel hide your cell or UIImageView hide it. It will automatically detects tableView didSelectRowAtIndexPath. Check it first you wont face any problems. Did you try to implement tableview in detail first

Display TextLabel Text Over Custom Cell Image

I am trying to display cell's textLabel text to be visible on top of an image that spans the entire cell. I created a custom cell subclass just to make the image width be equal to the entire cell size like so:
- (void) layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(0,0,320,80);
}
Now I would like to display my cells text on top of the cell. I do not have the cell text in the custom cell subclass, rather the code for it is in the TableViewController. Is there a way I can make the text display on top from within the view controller or do I have to put the text to be displayed in the custom cell subclass? Thank you
The best way is to make a custom cell, with an imageView and a label, and add them to the subview when the cell is initialized.
Then make sure they're hooked up to a property so you can configure the frame, as well as the content.
You can display the text in the TableViewController. Just make sure you set the image first before you set the text. I have provided code snippet in the method tableView:cellForRowAtIndexPath:
cell.imageView.image = theImage;
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 0.0,100.0, 30.0)];
textLabel.text = #"Testing";
[cell.contentView addSubview:textLabel];

Resources