UITableView - always select the row in the middle - ios

I have a UITableView with over 50 rows but only 5 are shown at a time. How can I force the middle row to always be selected? For example 2,3,4,5,6 are shown, 4 will be selected.
The user scrolls -> 14,15,16,17,18 , 16 will be selected etc'.
Thanks

This method worked pretty well for me. I had a table view that showed 5 rows at a time, but when I log visibleRows, I always got a count of 7, so I get the index paths for those seven, and select the middle one (index 3):
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSArray *vis = self.tableView.indexPathsForVisibleRows;
if (vis.count %2 == 1) {
[self.tableView selectRowAtIndexPath:vis[3] animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}

Use the tableView:willDisplayCell:forRowAtIndexPath: method that is part of the UITableViewDelegate protocol.
In here you can check the indexPath.row value, and if it is within your specified range, use cell.selected.
Further information:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html
Although I recommend you maintain whether or not a cell should be selected within your datasource, rather than hooking into the position of a cell, this way you can check your datasource if a cell should be selected rather than forcing yourself to use a particular index cell for a particular thing.
Hope this helps.

Purpose is UIPickerView custom i think,
Take a look here and this question will help you

Related

How to update a view added in UITableViewCell?

What I'm doing?
I'm adding dynamic views to my UITableViewCell (which isn't subclassed).
Cell Hierarchy :
UITableView > UITableViewCell > cell.contentView > MainView > ([Number Of PointView] + [Options View]).
Here it is:
When a user will tap "Add Another Point", I'll add a PointView (which will be same as above) :
Y position [ ] X position [ ]
which will be look like this,
What is my logic to get this done?
I'm taking MainView from cell.contentView.
Then fetch last two views (Point View & Options View) added into MainView.
Add Another PointView in MainView.
Update frames for newly added PointView based on last PointView and also update frame of OptionsView.
Resizing height of MainView
And reloading particular cell.
I'm able to get it work, to confirm I've logged frame and subviews for that MainView. But once I tap on "Add Another Point" button again, I found that MainView isn't updated at all?
My simple question is, if I have a MainView (which I fetch from a Cell), can I update it directly or not?
P.S. I already have a poor solution for this is, to remove MainView and recreate new – which I found unnecessary. Any thoughts?
Short answer, yes you can.
cell.contentView is a placeholder. What you are likely running into is an entirely different problem: UITableViewCells are cached, then recycled and reused by the system.
Anytime you respond to cellForRowAtIndexPath, you basically need to reset the content.
I think what you should do. Just make this one cell class
This one other cell subclass
Use this method to insert your rows and also update your data source for table view that is the number of rows in section part
Your + button on click method can be like this..
//Use your own logic what you want to do.
Update your data source
NSIndexPath* index=[NSIndexPath indexPathForRow:[table count]-1 inSection:0];
NSMutableArray* indexPathArray=[[NSMutableArray alloc]init];
[indexPathArray addObject:index];
//index will be your new added point cell. Updating your datasource
[_myTableView insertRowsAtIndexPaths:indexPathArray
withRowAnimation:UITableViewRowAnimationTop];
Make this YES
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}

Capturing Data from a tableView

There are hundreds of articles explaining how to set data to a tableview but I couldn't find any for getting data from it.
For example, if a have a tableview with 3 rows and in each row I have 3 textfields, what is the best way to get these textfields?
Xcode 61 doesn't let me to create static cells and if I create 3 prototype cells, it doesn't allow me to create 9 different textfields (so I could manage the info separately).
So I understand I have to create 3 textfields in a prototype cell and then differentiate each row, but how can I differentiate each row in the function "cellForRowAtIndexpath"?
I'd appreciate every other solution.
Thanks in advance.
You can set tag value to each textField from your "cellForRowAtIndexpath".
cell.TextField.tag = indexPath.row;
Take one mutable array.
Now when you textfield delegate method call, you can update or capture textFields value in array according to tag, and use for further process. Some thing like below.
-(void)textViewDidEndEditing:(UITextView *)textView
{
if([array count] > textView.tag)
{
[array replaceObjectAtIndex:textView.tag withObject:textView.text];
}
}
- (void)textViewDidChange:(UITextView *)textView
{
if([array count] > textView.tag)
{
[array replaceObjectAtIndex:textView.tag withObject:textView.text];
}
}
If you are not clear let me know.
best way I know of; if you're adding subview like a textField would probably be to tag them with restorationIdentifiers.
I think there is also a method for tableView that has something to do with accessoryViewDidChange or something
or add a target for the field and use didPerFormAction: maybe..
how did you configure the cell when you added the text field?

Scroll UITable to specific table item

So i have a UITableview with 3 different tabs setup.
I want to scroll the table to a specific item.
If user added a new item, table to scroll all the way to the bottom because that's where the item will be added.
If user picks one of the items and edits it, it should come back to the same position on the table. Not scrolled to the top or bottom.
This is what I have:
This is a class variable.
NSIndexpath lastScrollY;
In my ViewWillDisappear :
lastScrollY = this.tableView.IndexPathForSelectedRow() ;
In my ViewWillAppear:
if(lastScrollY != null) {
this.tableView.ScrollToRow(lastScrollY, UITableViewScrollPosition.None, true);
}
this does not seem to work.
And that is only for when user selects a saved item.
What about when then add a new item/row to the table?
Please explain in detail as I am new to this.
Thank you for your time!
For after you add a new cell to your tableView, use the same method you did before, but change the scroll position (this code is in Objective-C, but does the same thing as yours. I'll try to help with a translation if you need one):
[tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionBottom
animated:true];
You might also want to set lastScrollY in an override of the method
(void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
using the automatically passed in value indexPath to save to lastScrollY.
Lastly, I'm not sure if this will help, but in your current scrollToRow call, you have it scrolling to "none", try having it scroll to UITableViewScrollPositionTop instead. If that still doesn't work, try calling [tableView reloadData] at the end of your viewWillAppear method, which is probably something along the lines of tableView.reloadData() in your language.
Hope this helps!

iOS view similar to add/edit contact

I'm trying to create a view similar to add/edit contact in iOS and there are a few things that are happening, and I'm not sure how they are implemented. Any help in understanding is greatly appreciated.
For each section in contacts i.e. name, phone number, email, etc are these each their own tableview or are these sections within a larger tableview?
When clicking done when adding or editing a contact, the unused tableview cells disappear. Is this using deleteRowsAtIndexPaths:withRowAnimation: or is there a hide method I haven't found? If it is using that method, then when clicking the edit contact button, how does the view brings back these unused tableview cells?
When clicking on a cell in the tableview cell when editing a contact, you are able to change the text. Is this a textfield within a tableview cell or is it actually modifying the label of the tableview cell?
I am not looking for any specific code, as a fairly new programmer I am just trying to understand the strategies/best way to implement these features.
I tried a lot different ways to implement that. the easiest one: Subclass UITableViewCell and overwrite setFrame:. note that this is easy to achieve for grouped tables, but hard for plain ones. in the datasource's tableView:cellForRowAtIndexPath: create an object of this custom cell for the first section. use another identifier for cells of that section, so that only the correct cells will be re-used.
yes, I assume that. The controller has some sort of definition how many cells has to be show in edit mode and how many are actually used with some sort of information. you can easily create a array of indexPaths that must be deleted.
I would do it in tableView:didSelectRowAtIndexPath: by fetching the cell via tableView:cellForRowAtIndexPath:, hide the label and unhide or add a textfield and make this first responder.
code for 1.
the cell
#interface InsetCell : UITableViewCell
#property(nonatomic)CGFloat inset;
#end
#implementation InsetCell
- (void)setFrame:(CGRect)frame {
CGFloat inset;
if (_inset == 0) {
inset = 70; //default value
} else {
inset = _inset;
}
frame.origin.x += inset;
[super setFrame:frame];
}
-(void)setInset:(CGFloat)inset
{
_inset = inset;
[self setNeedsLayout];
}
#end
a project that uses similar code

How to access first and last cells of UITableView

I have a UITableView with custom cells,, I want to set background images of those custom cells. The background image of first and the last cell of the UITableView should be different than other cells.
I have used dequeueReusableCellWithIdentifier: to create the cell.
to set the background images of the first cell, I wrote
`
if (indexPath.row==0) {
cell.cellBackgrounfImage.image=[UIImage imageNamed:#"TopCellImage.png"];
}
`
1) Because of the reusing cell,,, this is going to set middle cell image rarther than the first cell. How can I overcome this problem
2) How to access the last cell of the UITableView to set other image
Please anyone give me a solutions for these 2 questions.
Thanks
For Step 1)
When you are scrolling tableview, it will be always reload data. So may be it will generate background image for middle one.
But, if you are set indexpath.row == 0 condition, then it will not affect any row instead of first one.
For Step 2)
You can get values for no of arrays (Array Count).
suppose int totalArr = [arr count];
Then apply condition same as first row by,
if(indexpath.row == (totalArr - 1) {
cell.cellBackgrounfImage.image=[UIImage imageNamed:#"BottomCellImage.png"]; }
So, it will work for last one also.
Thanks.

Resources