I am able to add a 'line separator' above my uitableviewcells manually (out-of-box iOS isn't cutting it for this). So I have created an array of UIViews, called _separatorLines, I add one UIView from each index to the uitableviewcells' contentView in my 'cellForRowAtIndexPath' with
UIView *v = [_separatorLines objectAtIndex:indexPath.row];
[cell.contentView addSubview:v];
however when i select a cell, i would like all other cells' line separators to turn red.
in 'didSelectRowAtIndexPath' I have:
{
for(unsigned int i = 0; i < _rowsInSection-1; i ++)
{
//make all other rows have a red content view
if(i != indexPath.row)
{
NSIndexPath *I = [NSIndexPath indexPathForRow:i inSection:1];
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:I];
UIView *separatingLineView = [_separatorLines objectAtIndex:I.row+1];
separatingLineView.backgroundColor = [UIColor redColor];
[cell.contentView bringSubviewToFront:separatingLineView];
[separatingLineView setNeedsDisplay];
[cell setNeedsDisplay];
[cell.contentView setNeedsDisplay];
}
}
[self setNeedsDisplay];
Instead I do not see the contentView of the uitableviewcells changing.
For me I think the issue was the line
NSIndexPath *I = [NSIndexPath indexPathForRow:i inSection:1];
UITableView starts on section '0' (just like the rows start on '0').
And also because my UITableView is the delegate, I stopped using
[self tableView:tableView cellForRowAtIndexPath:I];
and replaced it with
[self cellForRowAtIndexPath:I];
Not sure if that had anything to do with it
Related
I have a screen, where we are displaying questions with YES or NO. For Yes Or No we have used UISegment Control.
When a value is changes in the UISegment Control the scroll is moved to Top of the screen, when we have more questions and when we select last or but last as shown in the image.
Below is the code used to render the controls.
Link to the videos how the output looks https://imgur.com/nvn1Exw .
Please help me how can i remove the scroll movement on selection of new value in segment control.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return timeOutQuestions.count;
}
- (UITableViewCell* )tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
static NSString* questionTableIdentifier=#"SegmentCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: questionTableIdentifier];
if(cell == nil){
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:questionTableIdentifier];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
}
cell.separatorInset = UIEdgeInsetsMake(0.0 , cell.bounds.size.width , 0.0, -cell.bounds.size.width);
Question* currentQuestion=[[shared getQuestions:questionSet] objectAtIndex:indexPath.row];
#try{
UISegmentedControl *segment = [[cell.contentView subviews] objectAtIndex:0];
segment.selectedSegmentIndex = [[questionSegmentControl objectAtIndex:indexPath.row] integerValue];
[segment addTarget:self action:#selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
UILabel *label = [[cell.contentView subviews] objectAtIndex:1];
label.text = currentQuestion.questionText;
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}#catch(NSException* ex){
NSLog(#"%#",ex.reason);
}
if(changeOffSet && indexChangedValue.row > 2){
[self.questionTableView setContentOffset:tableContentOffSet animated:NO];
[UIView setAnimationsEnabled:true];
changeOffSet = false;
}
return cell;
}
- (IBAction)segmentSwitch:(UISegmentedControl *)sender {
NSInteger selectedSegment = sender.selectedSegmentIndex;
CGPoint senderOriginInTableView = [questionTableView convertPoint:CGPointZero fromView:sender];
NSIndexPath *indexPath = [questionTableView indexPathForRowAtPoint:senderOriginInTableView];
indexChangedValue = indexPath;
tableContentOffSet = self.questionTableView.contentOffset;
[UIView setAnimationsEnabled:false];
changeOffSet = true;
}
https://imgur.com/nvn1Exw
I think you are doing the scrolling programatically with this
[self.questionTableView setContentOffset:tableContentOffSet animated:NO];
I think you need to rework it a bit. Stop changing the content offset and see how it goes. Also, maybe you need to get rid of this line
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
which I think also interferes with the scrolling.
I am adding a expandale/collapsable UIView below UITableviewCell. I am able to add and hide the view on button action but unable to remove it when view is shown in another cell. I am updating the view height constraint to 0 on hiding.
Here's my code:
-(void)doneButtonClicked:(UIButton*)sender{
CGPoint buttonPosition = [sender convertPoint:CGPointZero
toView:_tableView];
NSIndexPath *indexPath = [_tableView
indexPathForRowAtPoint:buttonPosition];
CustomCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
for (int i = 0; i < [_expandedIndexPaths count]; i++){
if (i != indexPath.row && [_expandedIndexPaths count] > 0){
[self.expandedIndexPaths removeObject:indexPath];
//Animation effect for expanding/collapsing view
[cell animateClosed];
}
}
if (sender.tag == 0) {
[self.expandedIndexPaths addObject:indexPath];
[cell animateOpen];
//[self.expandedIndexPaths removeObject:indexPath];
}else{
[self.expandedIndexPaths removeObject:indexPath];
[cell animateClosed];
}
[_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
The constraint update should also happen in the cellForRow method. Set the constant to 0 there.
If you want the view to appear again for the cell that open it in first
place and didn't close it. Add hidden state boolean var in the cell
class and update every time you update that constant. Then on
cellForRow check the value of that boolean and set the constant
accordingly.
I have table with cells; each cell contain label with phone number. When i tap on the label, UIMenuController appears.
Is there a main way to find indexPath of cell, which contain selected label? (didSelectRowAtIndexPath: should not be called)
I can imagine many dirty hacks, but i looking for a main/good solution.
upd
I have reference to selected label.
Edit:
This is a better way.
- (IBAction)someMethod:(id)sender {
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
}
From your label, search for the cell object in the label's hierarchy:
-(UITableViewCell*) cellContainingView:(UIView*)view
{
UIView* currentView = view;
while (currentView)
{
if ([currentView isKindOfClass:[UITableViewCell class]])
{
return (UITableViewCell*)currentView;
}
currentView = currentView.superview;
}
return nil;
}
Once you have the cell, call [tableView indexPathForCell:cell]
The use of this method instead of hard coding view.superview.superview makes your code stronger because it handle possible changes in cell's view hierarchy through system's versions.
You could go:
NSMutableArray *cells = [[NSMutableArray alloc] init];
for (NSInteger j = 0; j < [tableView numberOfSections]; ++j)
{
for (NSInteger i = 0; i < [tableView numberOfRowsInSection:j]; ++i)
{
[cells addObject:[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
}
}
for (UITableViewCell *cell in cells){
for (UILabel *label in cell.subviews){
if (label == yourLabel)
indexPath = [tableView indexPathForCell: cell];
}
}
I have a working sample of UICollectionView, I can move a cell [by indexPath], but I need a specific cell [indexpath.row?] So I move the cell i need, At the moment a cell moves but not the correct one, so
when I move for example
NSIndexPath *index = [indexPaths objectAtIndex:0];
the cell on indexpath.row = 3 moves, how to call the cell by indexpath row?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor lightGrayColor];
CGRect frame = CGRectMake(10, 100, 300, 500);
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
self.collectionView=[[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];
[self.collectionView setDataSource:self];
[self.collectionView setDelegate:self];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
[self.collectionView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:self.collectionView];
UIButton *animus = [UIButton buttonWithType:UIButtonTypeRoundedRect];
animus.frame = CGRectMake(100, 20, 90, 30);
[animus setTitle:#"Animus" forState:UIControlStateNormal];
[animus addTarget:self action:#selector(animusPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:animus];
}
- (void)animusPressed:(id)sender
{
NSIndexPath *index = [indexPaths objectAtIndex:0]; //en el index!
NSLog(#"moving tha cell :: %d", index.row);
__weak UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:index]; // Avoid retain cycles
CGRect frame = cell.frame;
frame.origin.y = 300;
cell.frame = frame;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor=[UIColor greenColor];
UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, cell.bounds.size.width, 40)];
[cell.contentView addSubview:title];
NSString *titleLbl = [NSString stringWithFormat:#"i = %d", indexPath.row];
title.text = titleLbl;
return cell;
}
So a cell moves, but not the one I want, I need to move a specific cell, by indexpath.row like it is shown on the cellForRow,
How to specify what cell I want to move?
Please note im moving cell on index = 0 , but the indexpath.row = 2 is the one moving
Thanks
Have you tried something like this?
[NSIndexPath indexPathForItem:0 inSection:0]
First of all, it will be more difficult to make any adjustments your UICollectionView without using an array as your data source. Most commonly, you will have one array holding data and in your cellForItemAtIndexPath: method you grab index-specific data from that array via:
MyData *myData = [self.arrayOfMyData objectAtIndex:indexPath.row];
This way you can populate the cell using data related to the current indexPath. Once you do that, just update your data array and then call:
[self.collectionView reloadData];
Then the updates will be represented in the collection view. If you want to animate those changes, use:
[self.collectionView performBatchUpdates:^{
[self.collectionView reloadData];
} completion:nil];
If you want to "move" cells around with the appearance that it changed its original position, you could manage your arrayOfMyData to add a signifier at the index you want empty to set the background of the cell as clear and put the cell to move at the end of your array, then call reloadData.
I have created tableView with custom cells that contain image view and scrollView. All scroll views contain labels wider than screen bounds, so when I scroll to left/right I want all scrollViews to scroll in same direction. Problem is I don't know how to get reference to each scrollView in scrollViewDidScroll method.
my viewController class:
#import "EPGViewController.h"
#interface EPGViewController (){
NSArray *EPGList;
int scrollPositionX;
}
#end
#implementation EPGViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBarHidden = false;
EPGList = [NSMutableArray arrayWithObjects:#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12", nil];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return EPGList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *hlCellID = #"EPGCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:hlCellID];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:hlCellID];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
UILabel *label = (UILabel *)[cell viewWithTag:15];
label.text = EPGList[indexPath.row];
UIScrollView *scrolView = (UIScrollView *)[cell viewWithTag:16];
scrolView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
scrolView.delegate = self;
scrolView.tag = indexPath.row+101;
scrolView.scrollEnabled = YES;
scrolView.contentSize = CGSizeMake(scrolView.frame.size.width,scrolView.frame.size.height);
[scrolView setShowsHorizontalScrollIndicator:NO];
[scrolView setShowsVerticalScrollIndicator:NO];
int i=0;
for(i=0;i<15;i++){
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0+i*100, 0, 100, 100)];
label.text = #"HELLO";
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont fontWithName:#"ArialMT" size:18];
[scrolView addSubview:label];
scrolView.contentSize = CGSizeMake(scrolView.frame.size.width+i*label.frame.size.width,scrolView.frame.size.height);
}
[cell addSubview:scrolView];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 80.0;
}
- (void)scrollViewDidScroll:(UIScrollView *)callerScrollView {
scrollPositionX = callerScrollView.contentOffset.x;
//if this is called with table view exit
if (callerScrollView == self.tableView) return;
int indexPath = callerScrollView.tag-101;
NSLog(#"TAG: %d",indexPath);
for (UITableViewCell *cell in self.tableView.visibleCells) {
//TODO: Don’t use tags.
UIScrollView *cellScrollView = (UIScrollView *)[cell viewWithTag:16];
if (callerScrollView == cellScrollView) continue;
cellScrollView.contentOffset = CGPointMake(scrollPositionX, 0);
}
}
#end
EDIT:
I managed to solve my problem by deleting line where I add different tags to scrollviews in table view. now I just set offset to scrollview with tag 16.
There are multiple wrong things in your code. Here is better implementation:
- (void)scrollViewDidScroll:(UIScrollView *)callerScrollView {
scrollPositionX = callerScrollView.contentOffset.x;
if (callerScrollView == self.tableView) return;
for (UITableViewCell *cell in self.tableView.visibleCells) {
//TODO: Don’t use tags.
UIScrollView *cellScrollView = (UIScrollView *)[cell viewWithTag:16];
if (callerScrollView == cellScrollView) continue;
cellScrollView.contentOffset = CGPointMake(scrollPositionX, 0);
}
}
Some additonal notes:
This method will be called by table view too, so you will have to check that.
Don’t use any indexes, just plain iteration.
I used checking of scrollViews, but basically there it would be OK without it.
Using tags to identify subviews is not a good idea.
Don’t call reloadData every time!
You should use [tableView visibleCells]; in order to get all cells that are visible at this moment.
You could use UITableView's visibleCells method to get all visible cells and get your scrollviews from there (you can use viewwithTag like you do, but on all cells).
Then you would have to remember to adjust the scrollview position as cells are reused/recreated - probably keep the scrolled offset as a property of your View Controller.
The problem is that you're using dequeueReusableCellWithIdentifier rather than just looping through the cells that have already been displayed.
Do something like:
for (NSInteger j = 0; j < [tableView numberOfSections]; ++j)
{
for (NSInteger i = 0; i < [tableView numberOfRowsInSection:j]; ++i)
{
if (!(indexPath.section == j && indexPath.row == i))
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
UIScrollView *scrollView = (UIScrollView *)[cell viewWithTag:16];
scrollView.contentOffset = CGPointMake(position_x, scrollView.frame.size.height);
}
}
}