I have a UIButton in a UICollectionViewCell.
When the UIButton is pressed, I clear the selected state and then remove that cell from the UICollectionView using deleteItemsAtIndexPaths. This works perfectly except for one thing.
After the cell is deleted, the cell underneath slides up. That cell's UIButton selected state changes from selected -> unselected. Seems like it's picking up the selected state from the original cell?
Found the problem...
I tried to remove the cell first, but then used the indexpath to unselect the button.
Problem is that because the cell was removed, the indexpath pointed to the cell underneath..
dumb order of operations bug
You should use a model to control the button's state, because the cell will reuse
every cell for indexpath should be binded with a model.
// control button selected state
#property (nonatomic, assign) BOOL isSelected;
cell.button.selected = model.isSelected,
Related
I have added UIButton(for some functionality requirement in project.) with checkbox image. Checkbox image select and unselect working proper on UITableViewCell tap but if I tap cell on checkbox button area button image isn't changing. I mean didSelectRowAt method isn't calling.
I found solution. I have checked that UIButton's UserInteraction ignores UITableViewCell's tap.
So I have just added below line in my code. And cell tap working good.
cell.btnCheck.isUserInteractionEnabled = false
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:
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
I have a UITableViewController implementing custom cells via
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
and in the CustomTableViewCell.h file I declared
#property (nonatomic, weak) IBOutlet UISlider *slider;
which is connected to my slider in my storyboard and an IBAction that changes the background of the cell when the slider is adjusted (value changed). My problem is that when I slide down the table view, as soon as my cell is dequeued it is reused with the slider in the same position, and therefore the background an adjusted color. I don't want to keep the entire table in memory, but I need to keep the information input into a cell available for future use. Is there a standard way to handle a problem like this? Thanks.
You can put your slider values into an array, and the dequeueReusableCellWithIdentifier method, you can set the cell slider value from the array
Yes you need to store data for the table in memory (for example slider values in NSArray) and in your UITableView datasource method you need to update state of reused cell. UITableView is smart enough not to load cells for whole table, it loads only visible cells. It is the standard way to do it.
why not configure the cell after method dequeueReusableCellWithIdentifier
cell.backgroundColor = //adjust the color
cell.slider.value = //adjust value
I have a subclass of UITableViewCell, which has a number of UILabels, a UIImageView and a UIView in it.
I have the cell set to highlight blue when it is selected. The strange behaviour that I am observing is the UIView that I have added as a property of the UITableViewCell subclass disappears when the cell is selected, and returns when the cell is deselected. This UIView is just an empty view with a background color, so it is possible that the background color is being set to clear when the table view cell is selected. None of the other elements are effected.
Does anyone have an idea what is happening here, and how to fix it?