UITableView over UITableViewCell didSelectMethod - ios

This must be stupid question,
Some how I know the reason but difficult to find solution.
So any help will be welcome..
What I am having is, one UITableView with header.
Header contains the Question and UITableViewCell contains the options to question.
Each cell is having 3 controls,
2 Labels and 1 button.
1 label contain Option and other contains its answer.
To match the answer you have to select option from drop down, which is opened when button from that cell is clicked.
That drop down is again a UITableView.
Which options.
So that DropDownTableView is added over UITableViewCell.
but problem is that when I open drop down on that cell, the table goes beyond boundary of cell,
So DidSelectMethod of DropDown table is not getting called.
I can't increase the height of BaseTableVieCell, but I want to select option from DropDownTable.
What should I do.
I tried bringSubviewToFront, and also disabling the baseTableView when Button is clicked.
But didn't helped.
Is there any other way to achieve the task.
Thanks for help.
Here is the picture which will give detail idea.
I am not able to select option "A", "B" , "C" as they go out of boundary of cell.
Thanks in advance.

You just need to resize the cell whose button is clicked as follows:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CGRect rect = cell.frame;
rect.size.height = 150.0;
ht = 150.0; // declared previously. you may check in the attached project
selected = indexPath.row;
[cell setFrame:rect];
[tableView beginUpdates];
[tableView endUpdates];
}
I have just given you an idea on how you will resize the cell. Here I have taken a static height(150.0). Instead of that you will have to give the height of your inner tableView that gets dropped down on UIButton click.
You can find demo project to change the cell height dynamically here.
Hope that helps.

Related

What is difference between deselectRowAtIndexPath and cell setSelected?

What is difference between
[tableView deselectRowAtIndexPath:indexPath animated:NO];
and
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexpath];
[cell setSelected:NO];
???
In a UITableView a cell does not match a specific piece of your data. It can (and should most of the time) be reused and it is nil when not visible.
deselectRowAtIndexPath will set the indexPath as 'not selected' in your tableview, so when you scroll back and forth to that cell, it will stay unselected, because you told your tableview that whatever the cell you display at that indexPath it should be unselected.
With UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexpath];
[cell setSelected:NO]; you set the cell as unselected. However that cell can be used for other pieces of data, and it can even be nil if this indexPath is not displayed.
First one is programmatically deselecting the cell. So it will un-highlight the cell if the user has selected it already.
As for the second bit of code, I believe that is just a pointer or reference to one of the cells in your table view. You can use this code to edit a cell OUTSIDE of any of the table view delegate methods. So if you wanted to edit/update a label on one of your cells but from a random method (not a table view delegate method), then you could use that code to reference the label text property.
I think you should also check out the Apple Developer Library website. It explains all the different table view method/properties/etc in lots of detail:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/
The concepts of "selected" is different for a tableview and for a cell. The tableview can have one or more rows selected that changes the state of the tableview, however changing the tableview cell selected state only affects its appearance and nothing else.

Subviews not clipped on swipe left to delete UITableViewCell

I'm simply trying to implement swipe left and hit the delete button to remove a UITableViewCell.
On touch, the cell expands/contracts. The full size of the cell has been designed using Xcode and I have "Clip Subviews" ticked in order to prevent the bottom of the cell appearing in the contracted state.
However, when I swipe left on the contracted cell (to show the delete button), the bottom of the cell reappears! I've searched around for a solution but have yet to find one. Any help would be appreciated. Thanks.
Full credit to Vitaliy Gozhenko, I am just duplicating an answer here. See this SO answer.
The issue is that cell.contentView.clipsToBounds is getting set to NO when the table enters edit mode.
To fix
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.contentView.clipsToBounds = YES;
}
for me this was the fix needed for iOS8.

Change sub view of all cells in UITableView

I'm creating an app which contains a screen that shows a table view with custom cells. Each cell contains two labels and a subview, which further contains other subviews. I'm handling the click event on the cell to hide/show the subviews within the subview in the cell. How can I make it so that when I click on a single cell, the subview of all the cells will change?
It is like the Stock application in iPhone (using iOS 7), here is a screenshot:
As in the image above, when you click on any of the green box, all the boxes change to reflect the same type of value.
Please let me know if this approach is fine, or how this can be implemented.
There are a couple ways of doing this. The first that comes to mind would be to handle the different states within the UITableViewCell subclass, and just reload the visible cells:
[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationAutomatic];
If you're looking for more control over the process though, this process could also be achieved by changing the state future cells should load into, and then calling a method on every visible cell. This would provide you with an easy way to have complete control over how the contents of the cell update.
// Change flag for cell state then...
for (NSIndexPath *indexPath in [self.tableView indexPathsForVisibleRows]) {
if (condition) {
MyCellSubclass *cell = (MyCellSubclass *)[self.tableView cellForRowAtIndexPath:indexPath];
[cell someMethodWithArg:(id)state];
}
}
To do something as in Stock app you should handle two method cellForRowAtIndexPath: and click action method.
In cellForRowAtIndexPath: you should do the check which cell/button was pressed and display value base on it:
//Pseudo code
//cellForRowAtIndexPath
if (cellNo3Pressed)
{
//set up text with the right value.
}
else if (otherCell)
{
//set up text with the right value.
}
This will handle the cell which are not visible on the screen.
The next action method should handle nice animation on all of the visible cell:
NSArray *paths = [tableView indexPathsForVisibleRows];
for (NSIndexPath *path in paths)
{
UITableViewCell *cell = (UITableViewCell *)[self.tableView cellForRowAtIndexPath:path];
//Animate changes for cell
}

UILabel in a UITableViewCell With Dynamic Height

I'm having some problems implemented dynamic row heights in a UITableView - but it isn't the cells that I'm having a problem with, its the UILabel inside of the cell.
The cell just contains a UILabel to display text. My tableView:heightForRowAtIndexPath: is correctly resizing each cell by calculating the height of the label that will be in it using NSString's sizeWithFont: method.
I have a subclass of UITableViewCell that just holds the UILabel property that is hooked up in storyboard. In storyboard I've set its lines to 0 so it will use as many lines as it needs, and I've set its lineBreak to Word Wrap.
Here is how I'm setting up the cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ExpandCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
SomeObject *object = self.tableObjects[index.row];
cell.myLabel.text = [object cellText];
[cell.myLabel sizeToFit];
return cell;
}
When I build this, I get my table view with the cell's all sized to the correct height for their content, but the labels are all 1 line that just runs off the side of the cells. However, if I scroll the table so cell's leave the screen, and then scroll back to them, their label will be resized correctly and the cell will look how I expected it to initially.
I have also attempted calculating the labels frame with the same method I'm calculating the row height with, and I get the same behavior - it doesn't draw correctly until it scrolls off of the screen and back on again.
I have found two ways to work around this, and neither are acceptable solutions.
First, if in viewDidAppear: I call reloadData on my tableview, the cells and labels draw themselves correctly the first time. This won't work for my situation because I will be adding and removing cells to this table, and I don't want to call reloadData every time a cell is added.
The second workaround seems very strange to me - if I leave the font settings at the default System Font 17 on the UILabel, the cells draw themselves correctly. As soon as I change the font size, it reverts to its behavior of not drawing a label correctly until it leaves the screen and comes back, or gets reloadData called on the tableView.
I'd appreciate any help with this one.
I ended up resolving this by alloc/init'ing the label in cellForRowAtIndexPath. I'm not entirely sure why this is a solution - but it appears the problem I was experiencing has to do with how storyboard (or when, perhaps?) creates the objects within the cell. If I alloc/init the label in the cell in cellForRowAtIndexPath, everything loads and sizes correctly.
So... my current fix is to check if the cell has my custom label in it. If it doesn't, I alloc/init the label and put it in the cell. If it does have one, as in its a cell that's been dequeued, then I just set the text in the label that is already there.
Not sure if its the best solution, but its working for now.
I ended up resolving this by unchecking the AutoSizing checkbox in IB. It is unclear why auto-layout was causing this problem.
I ran over the same problem and I end up solving it by calling [cell layoutIfNeeded] before return the cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ExpandCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
SomeObject *object = self.tableObjects[index.row];
cell.myLabel.text = [object cellText];
[cell layoutIfNeeded];
return cell; }

addSubview to specific uitableviewcell on select

I have a tableview and I want to add an imageview to the contentView of the cell at the row it was selected in.
This is my current code and when I click any cell it only adds the imageView to the last row rather than the row I click on.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
playIcon = [[UIImageView alloc]initWithFrame:CGRectMake(20, 22, 9, 12)];
UIImage *image = [UIImage imageNamed: #"cell_play.png"];
[playIcon setImage:image];
[cell.contentView addSubview:playIcon];
}
You can do like this
[[tableView cellForRowAtIndexPath:indexPath] addSubview:playIcon];
First, you have not identified a cell at a particular row. Since your compiler didn't complain, I assume that cell is an ivar. It is pointing to the last cell that it was pointed to, which very well may be the last cell in the table.
You have to be careful that you don't set the content of a cell outside of the UITableview methods, including the UITableView method cellForRowAtIndexPath:`. This is described in a recent answer I provided at this link.
So if you looked at that answer, you will have read that the tableview methods make sure the cell is rendered correctly even when the tableview scrolls and cells appear and disappear. I recommend that you set up your data source with some kind of tag or property that you can use to see that this image needs to be present. That way, it will be there even if you scroll the cell so it dissappears and reappears.
work for me with UIcollectionView and UItableviewcell in Swift 2.0:
CV_Posts.cellForItemAtIndexPath(index)?.addSubview(imag_curtir)

Resources