I want to get a NSString out of the table HeaderView that's on top that moment. Because I want to implent it in ScrollViewDidScroll so one label changes when the header view changes.
Didn't find any clues on the web how to do this
Thanks!
There is no predefined method by which you can get top section. There is one trick by which you can achieve it. First get indexPath for all visible cells.
NSArray *visible = [tableView indexPathsForVisibleRows];
NSIndexPath *indexpath = (NSIndexPath*)[visible objectAtIndex:0];
Now you can get section value by indexpath.section. After getting this section you can get string from your data array for this section.
Related
I've been using the code below to access the value of a textfield in my custom UITableViewCell. Problem is, this only works for cells that are visible at the time the method is called.
NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:3];
AppointmentNotesTableViewCell *cell = [self.tableView cellForRowAtIndexPath:path];
NSString *str = cell.notesView.text;
Does anyone have a better way to access this information, regardless of the cell being visible or not?
That is because invisible cells do not exist. Cells get reused when you scroll the table view. Only those of them, visible on the screen can actually be accessed.
All the objects that potentially displayable in the table view are normally stored in some kind of list. What you need is to access the object from the list using the index you have from #path instead of trying to access its "rendered copy" from the table view itself.
I have custom cell having switch in few cells at right side. what I want is to store value of specific cell on switch change event. Table view has number sections so I can't set tag for switch because I need section as well as row to obtain index path.
Any suggestion any alternative but I have to use UISwitch in section based table view.
Thanks
In your custom cell add properties which help you identify the information the cell represents. Index path, indexes for your data model etc...
Then add a block property to the cell which you can call to tell a UITableView or any other piece of code when a cell switch changes. e.g.
#property (nonatomic,copy) void (^onSwitchChange)(UITableViewCell *cell);
Inside your custom cell code, add an action handler for the UISwitch. When it fires, call self.onSwitchChange(self) which will notify the code which registered an onSwitchChange block that a switch has changed and on which cell.
In your table view when you create the cell, set the onSwitchChange block as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath
{
<snip>
YourUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:yourCellIdentifier forIndexPath:indexPath];
cell.onSwitchChange=^(UITableViewCell *cellAffected){
// Add code to deal with the swicth switch using properties of cellAffected
... Your handler code here ...
}];
<snip>
}
This lets you handle all the changes in the table view controller. Hope this helps.
The answer from #Jageen works. I had to find out which superview the cell is, mine was one more level higher.
UITableViewCell *cell = (UITableViewCell*)[sender superview].superview.superview;
NSIndexPath *indexPath = [self.myTableView indexPathForCell:cell];
NSLog(#"Section %ld Row : %ld",(long)indexPath.section,(long)indexPath.row); // print row section and index
You can still create tags even if you have sections if you have some idea of about max rows in a largest section. For example if you think there will be 1000 rows in a section then you can create tag using following formula.
tag = section * 1000 + row;
later in your IBAction of switch you can find out the indexpath (section and row) using following:
section = tag/1000;
row = tag%1000;
If you have no idea of how many rows your section will have you can find out the cell using sender.superview.superview (be careful if have added any other views in hierarchy).
Rory McKinnel's answer is still the cleanest solution for your problem.
I am trying to trigger some code based on if the cell I selected becomes the first or last cell on the users screen. I'm not trying to capture the index value of the data array. Just the index value of the cell that is visible on the screen. I'm sure this to create an array of visible cells.
NSArray *indexPathsForVisibleRows = [myTableView
indexPathsForVisibleRows];
But I keep hitting a dead end trying to then capture an index value based on that array.
I tried to use a CGPoint and convert that, but I keep getting an error. Any insight would be most helpful!
As per the documentation for the return value of that method:
An array of NSIndexPath objects each representing a row
index and section index that together identify a visible row in the
table view. Returns nil if no rows are visible.
The array returned from that method contains NSIndexPaths.
NSArray *indexPathsForVisibleRows = [myTableView indexPathsForVisibleRows];
for(NSIndexPath *eachIndexPath in indexPathsForVisibleRows)
{
NSInteger row = eachIndexPath.row;
NSInteger section = eachIndexPath.section;
UITableViewCell *cell = [myTableView cellForRowAtIndexPath:eachIndexPath];
if(cell.isSelected)
{
// this is our selected cell
}
}
I have a view with a map and and list displayed at the same time. I would like the map to move when the user scrolls the list.
I was hoping for something like
(UITableView*)tableView didScrollTableViewCellToTop:(UITableViewCell *)cell
I haven't found something that will support this yet.
Thanks in advance
You can get all the visibleCells by running...
NSArray *cells = [self.tableView visibleCells];
This returns an array of UITableViewCells.
You can then find the one with the lowest indexPath.row value to find the top one.
Remember that the UITableView is simply a scroll-view. So you can get the offset of the scroll view and use the position to get the cell at that location:
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: point];
To continuously monitor and change, implement a scroll-view-delegate and implement the scrollViewDidScroll method.
I have a table view with custom cells. Cells have an image view, a label and a progress view. What I want to achieve is to update progress view in a specific cell according to the event that fires. My idea is to find the cell by name label that is in the event and then update progress view. This may not be an efficient way but I couldn't think of some other way. Other ideas are also appreciated. Thanks in advance.
You could use a NSDictionary with the name as the key and the NSIndexPath as the object. Then to can look up the index path for a given cell using the name in the dictionary.
UITableView has a 'cellForRowAtIndexPath:` method that should give you the cell for a specific row.
i.e. something like
// Get visible cell for data item
NSInteger row = [myArrayOfThings indexOfObject:thing];
NSIndexPath *inedexPath = [NSIndexPath alloc] initWithRow:row section:0];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indeexPath];
// Update cell here ...
...
Disclaimer : Code typed from memory - you may have to look at the documentation for some of the method names :)
the more better way is with tag,
you can give each cell a unique tag and access that cell by this method
//method of UIView(UIViewHierarchy)
- (UIView *)viewWithTag:(NSInteger)tag;
// can be used like this
UITableViewCell *cell = (UITableViewCell*)[tableView viewwithtag:tag];