iPhone: UITableView with custom cells for settings - iphone

I want to use a UITableView for my settings screen. So that my settings view will look like apple does it, with a group table view, and then the UI elements to let you change settings.
So, it does not seem like the cells are loaded from an array, but seems like each one is customized.
Ideas on how to do this?

For starters, set the tableViewStyle property to UITableViewStyleGrouped.
Then change the data source to provide 2D arrays with the groups you want, instead of a simple array. It's pretty simple, actually.
You will need to customize the row with UIControl types that you want - I assume you're already doing that though.
EDIT:
To add the control to the row, create it when you create the cell.
...
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0.0f, 5.0f, 30.0f, 30.0f)];
[button setTitle:#"Click Me!" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
...

You can do it without an array. Just use the following methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
and then can use a switch-method to parse thru all rows and sections. Those two lines on the header of the methods on top and you can short it up a bit.
NSUInteger row = [indexPath row];
NSUInteger section = [indexPath section];
Don't forget, that manual tableviews are prone to errors.
cheers Simon

Related

iOS 7, UITableView and incorrect separator lines

I have a problem in iOS 7 with UITableView (style: UITableViewStyleGrouped) and separatorInset. At times, the separator is not visible.
Briefly, using the same table (exactly the same code) if I render the table loading directly all data (for example from a NSArray), separator lines are correct (default iOS7 style, correct inset value). If I dynamically add new rows to the same table using something like insertRowsAtIndexPaths, separator lines span the full width of the cell/screen (so, no iOS7 default style).
I tried to force 'separatorInset' using setSeparatorInset in both UITableView and in every single cell but this did not worked. If I reloadData after adding a new row, the separator lines are correctly shown.but this seems not a great solution.
Any ideas why separator is intermittently not visible?
Write this code in your ViewDidLoad method :
[tblView setSeparatorInset:UIEdgeInsetsZero];
For me the only things working is reload the previous cell, i.e. the one that is on top of the selected cell.
if (indexPath.row > 0) {
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section];
[tableView reloadRowsAtIndexPaths:#[path] withRowAnimation:UITableViewRowAnimationNone];
}
Apply your color in this method and this method into your code. this is iOS7 update.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}
Make sure clipsToBounds is set to YES for the cell, but NO for the cell's contentView. Also set cell.contentView.backgroundColor = [UIColor clearColor];
use this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([tableView respondsToSelector:#selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
}

UITableView section clicked in Viewforheaderinsection

I have a created custom UIButton in UITableView viewForHeaderInSection. Now when i click on the button within the section how will i know which section is clicked? Please help.
when you add button in viewForHeaderInSection , just set tag with section number like bellow.,..
button.tag = section;
and when your button clicked at that time you can get that number with bellow code...
- (IBAction)yourButton_Clicked:(id) sender
{
NSLog(#"Section Number => %d",[sender tag]);
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIbutton *btn =[[UIButton alloc]init];
btn.tag=section;// this tag property will differentiate button for different section
}
by this you can access btn and add event on that burron
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIbutton *button =[[UIButton alloc]init];
button.tag=section;// this tag property will differentiate button for different section
}
In didselectrowatindexpath check for if(index path.section) is equal to the section you want the action.

Get row number of UITableView in IOS [duplicate]

This question already has answers here:
get section number and row number on custom cells button click?
(5 answers)
Closed 9 years ago.
In my code I have a table view with many sections sorted alphabetically. I have checkboxes(UIButton) in table rows and I need check them according to respective status entered in row. I had done
checkbutton.tag = indexpath.row;
but when table scrolls and section get changed and I get status of previous section in my checkbox method. anyone please help me on this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CheckButton = [UIButton buttonWithType:UIButtonTypeCustom];
[CheckButton addTarget:self action:#selector(CheckBoxAction:) forControlEvents:UIControlEventTouchUpInside];
CheckButton.tag=indexPath.row;
//some other stuff
[cell.contentView addSubview:CheckButton];
}
and this my CheckBoxAction
-(void)CheckBoxAction:(UIButton *)btn
{
tag = btn.tag;
NSDictionary *tagdict =[statusArray objectAtIndex:tag];
}
Now, problem is that indexpath.row is 0 whenever section is changed, so I can't able to access exact row number in my CheckBoxAction.
use this for getting current row you tap.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CheckButton = [[UIButton alloc] init];
[CheckButton addTarget:self action:#selector(CheckBoxAction:) forControlEvents:UIControlEventTouchUpInside];
CheckButton.tag=indexPath.row;
[cell addSubview:CheckButton];
}
-(void)CheckBoxAction:(id)sender
{
// Cast Sender to UIButton
UIButton *button = (UIButton *)sender;
// Find Point in Superview
CGPoint pointInSuperview = [button.superview convertPoint:button.center toView:self.tableView];
// Infer Index Path
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInSuperview];
// Log to Console
NSLog(#"selected Row : %d", indexPath.row);
}
// Try this
CheckButton.tag = [[NSString stringWithFormat:#"%d%d",indexPath.section,indexPath.row] integerValue];
or
// try this below link to get logic for, how to handle checkbox in UITableView
Set checkmark in UITableView
What you are doing is wrong in many ways. So, I am not going to give you a solution for the exact problem you have, but more of a guideline:
First you are adding the checkbox directly to the cell, which means you are possible adding multiple CheckButton to the same cell, if you scroll your UITableView a few times.
Add the checkButton on the cell directly instead of keep adding it in the cellForRowAtIndexPath, this can give you a performance boost.
Use a block that will be executed when a user clicks the button. This way you are able to capture the context of your UIViewController. You should assign that block to the cell on cellForRowAtIndexPath

Table HeaderView iPhone

I have a UITableView that needs to have something inserted at the top in some cases (not always).
The code (if it were to be a NORMAL) cell would be as follows
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,98,100)];
imgView.image = [UIImage imageNamed:#"image.png"];
cell.imageView.image = imgView.image;
//sets cell text
cell.textLabel.text = #"Number";
but I was told on here that it would be easier to implement a headerView than to change the indexPath each time.
So my question is how would I implement this? Would I use - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section or would I use - (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section? Keep in mind, I have an image to place into the cell (headerView).
Thanks
You would use
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
titleForHeaderInSection: only sets the header as text, where as viewForHeaderInSection: will allow you to add a view, UIImageView, etc.
UIImageView is a subclass of UIView, so it can be returned from
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
This is what you should do since it allows full customization of the header image.
If you instead use
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section
then the only thing you have control over is the NSString that you return. You cannot add a background, change the font color, etc. Since you have an image, use the former method.

how to add a button without using custom cell on a UITableView?

How do I add a custom button on a UITableViewCell, and then delete the cell with that button without using Interface Builder and Custom Cell?
If you really want to add a custom button WITHOUT subclassing, just add the button to the contentView of the cell:
[cell.contentView addSubview:customButton];
You can set all the characteristics of the button: frame, target, selector, etc... Ad then used the above call to add it to the cell.
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame=//whatever
[customButton setImage:anImage forState:UIControlStateNormal];
[customButton setImage:anotherImage forState:UIControlStateHighlighted];
[customButton addTarget:self action:#selector(delete) forControlEvents: UIControlEventTouchUpInside];
//yadda, yadda, .....
You can tag it as well
customButton.tag = 99999;
So you can find it later:
UIButton *abutton = (UIButton*) [cell.contentView viewWithTag:99999];
You will need to decide WHEN to add the button, maybe on cell selection, maybe in editing mode... just put the code in the delegate method of your choice.
If the button's sole purpose is to offer deletion you should look into UITableViewDataSource which has a method called - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath. Implement it like so:
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
And then implement:
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Database removal code goes here...
}
To use these methods, let your UITableViewController implement the UITableViewDataSource protocol by doing something like:
MyClass : UITableViewController <UITableViewDataSource>
in your header file, and be sure to remember to set the viewController's datasource to self.