I m using grouped table view in my app. I have 2 sections in table. first section has 3 rows and second section has more than 10 rows .when I scrolled sections it displays rows from section 1 after 5 th row of section second. what should I do.
Here is my code...
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
switch (section) {
case 0:
return 3;
break;
case 1:
return 8;
break;
default:
break;
}
return 5;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.section==0)
{
if (indexPath.row==0)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
[Name setText:#"First Name "];
[cell.contentView addSubview:Name];
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
textName.placeholder=#"Enter First Name";
[textName setBorderStyle:UITextBorderStyleNone];
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
[cell.contentView addSubview:textName];
}
if (indexPath.row==1)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 110, 20)];
[Name setText:#"Middle Name"];
[cell.contentView addSubview:Name];
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
textName.placeholder=#"Enter Middle Name";
[textName setBorderStyle:UITextBorderStyleNone];
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
[cell.contentView addSubview:textName];
}
if (indexPath.row==2)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
[Name setText:#"Last Name "];
[cell.contentView addSubview:Name];
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
textName.placeholder=#"Enter Last Name";
[textName setBorderStyle:UITextBorderStyleNone];
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
[cell.contentView addSubview:textName];
}
}
if (indexPath.section==1) {
if (indexPath.row==0) {
cell.textLabel.text=#"1";
}
if (indexPath.row==1) {
cell.textLabel.text=#"2";
}
if (indexPath.row==2) {
cell.textLabel.text=#"3";
}
if (indexPath.row==3) {
cell.textLabel.text=#"4";
}
if (indexPath.row==4) {
cell.textLabel.text=#"5";
}
if (indexPath.row==5) {
cell.textLabel.text=#"6";
}
if (indexPath.row==6) {
cell.textLabel.text=#"7";
}
if (indexPath.row==7) {
cell.textLabel.text=#"8";
}
}
return cell;
}
You need 2 Cell identifiers. The problem is that you dequeue cells with the same identifier.
Paste the code and should work fine:D
P.S: I did not resolve your problems with your labels. You alloc them each time you display an cell. That is wrong.
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSInteger returnValue = 5;
switch (section) {
case 0:
{
returnValue = 3;
break;
}
case 1:
{
returnValue = 8;
break;
}
default:
break;
}
return returnValue;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifierSection1 = #"Cell1";
static NSString *CellIdentifierSection2 = #"Cell2";
UITableViewCell *cell;
if (indexPath.section==0)
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierSection1];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierSection1] autorelease];
}
if (indexPath.row==0)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
[Name setText:#"First Name "];
[cell.contentView addSubview:Name];
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
textName.placeholder=#"Enter First Name";
[textName setBorderStyle:UITextBorderStyleNone];
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
[cell.contentView addSubview:textName];
}
if (indexPath.row==1)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 110, 20)];
[Name setText:#"Middle Name"];
[cell.contentView addSubview:Name];
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
textName.placeholder=#"Enter Middle Name";
[textName setBorderStyle:UITextBorderStyleNone];
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
[cell.contentView addSubview:textName];
}
if (indexPath.row==2)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
[Name setText:#"Last Name "];
[cell.contentView addSubview:Name];
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
textName.placeholder=#"Enter Last Name";
[textName setBorderStyle:UITextBorderStyleNone];
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
[cell.contentView addSubview:textName];
}
}
if (indexPath.section==1) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierSection2];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierSection2] autorelease];
}
if (indexPath.row==0) {
cell.textLabel.text=#"1";
}
if (indexPath.row==1) {
cell.textLabel.text=#"2";
}
if (indexPath.row==2) {
cell.textLabel.text=#"3";
}
if (indexPath.row==3) {
cell.textLabel.text=#"4";
}
if (indexPath.row==4) {
cell.textLabel.text=#"5";
}
if (indexPath.row==5) {
cell.textLabel.text=#"6";
}
if (indexPath.row==6) {
cell.textLabel.text=#"7";
}
if (indexPath.row==7) {
cell.textLabel.text=#"8";
}
}
return cell;
}
Edit:
You can use different identifier for each type of cell. So if you have 2 types of cell you use 2 identifiers if you have 1 type use one identifier and so on. The identifier is used to dequeue a specific type of cell, if no cell can be reused it will simply alloc a new one.
Related
My problem is: I have 10 records and displaying them in UITableview with 10 rows. This is working fine in all version below iOS 10 but in iOS 10 all cells are overlapping with one another at top position of tableview (at row 0 position).
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
#try
{
for (UIControl *subview in cell.contentView.subviews) {
[subview removeFromSuperview];
}
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
NSString *storedValue, *currValue;
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 44)];
switch (headerTag)
{
case 0:
{
//conditional code
}
break;
case 1:
{
//conditional code
}
break;
case 2:
{
//conditional code
}
break;
.
.
.
.
default:
break;
}
lbl.lineBreakMode = NSLineBreakByTruncatingMiddle;
lbl.textColor = PRIMARY_LABEL_COLOR;
lbl.backgroundColor = indexPath.row & 1 ? DROPDOWN_SECOND_COLOR : DROPDOWN_FIRST_COLOR ;
[cell.contentView addSubview:lbl];
if([currValue isEqualToString:stringvalue])
{
UILabel *lblTick = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 44, 44)];
[lblTick setTextColor:PRIMARY_LABEL_COLOR];
[lblTick setBackgroundColor:[UIColor clearColor]];
[lblTick setFont:[UIFont systemFontOfSize:30]];
[lblTick setText:#"\u2713"];
[cell.contentView addSubview:lblTick];
}
}
#catch (NSException *exception) {
CATCH_NSException
}
return cell;
}
I did search on this issue and found so many answers but all are related to xcode 7 and iOS 8 and implemented same but its still not working.
Any help would be appreciated.
Thanks.
Hi I am very new in iOS and I have created one tablelist on my Viewcontroller and here on my tablelist row I have added one UIlabel programatically, ok that's fine.
And I have added one UIbutton programatically on my "self.view".
Here my main requirement is when I click that button, I want to change UIlabel background color which was added on my tablelist row.
For this I have tried the code bellow, but UIlabel backgroudcolor is not changing.
Please help me.
my code:-
#import "ViewController.h"
#interface ViewController (){
UILabel *label;
}
#end
#implementation ViewController {
UITableView *tableView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self tablelistCreation];
}
//UItableView createtion
-(void)tablelistCreation{
tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, 300, self.420) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
//Delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];
label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
return cell;
}
//UIbutton createtion
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button .backgroundColor = [UIColor orangecolor];
button.frame = CGRectMake(100, 440, 30, 30);
[self.view addSubview:button];
//button action
-(void)Method:(id)sender{
label.backgroundColor = [UIColor redcolor];
}
Your question was not clear if you want to change the label color on all cell or just some.
Anyway this color change must be made cellForRowAtIndexPath which is where the update / creation of the cell is made.
You can create a variable to inform if you want the color change or not and it check this on cellForRowAtIndexPath.
#implementation ViewController {
UITableView *tableView;
bool changeColor;
}
-(void)Method:(id)sender{
changeColor != changeColor;
[tableview reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];
UILabel label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
[cell.contentView addSubview:label];
}
if (changeColor){
label.backgroundColor = [UIColor redcolor];
}else{
label.backgroundColor = [UIColor clearColor];
}
return cell;
}
if you want change especific labels, you can create a nsarray to control what cell will be change
#interface ViewController (){
UILabel *label;
NSMutableArray *lableArray;
}
#end
#implementation ViewController {
UITableView *tableView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self tablelistCreation];
lableArray=#[].mutableCopy;
}
//UItableView createtion
-(void)tablelistCreation{
tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, 300, self.420) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
//Delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
[lableArray addObject:label];
}
cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];
//
// label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
// label.backgroundColor = [UIColor clearColor];
// [cell.contentView addSubview:label];
return cell;
}
//UIbutton createtion
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button .backgroundColor = [UIColor orangecolor];
button.frame = CGRectMake(100, 440, 30, 30);
[self.view addSubview:button];
//button action
-(void)Method:(id)sender{
[lableArray makeObjectsPerformSelector:#selector(setBackground:) withObject:[UIColor redColor]];
// label.backgroundColor = [UIColor redcolor];
}
cell is Reusable so creation should put in cell == nil condition,at every run into the condition will creation a new UILabel ,if you want change all of them ,you should put them in a collection ,such as NSMutableArray...
import "ViewController.h"
#interface ViewController ()
{
UILabel *label;
BOOL isTouchButton;
}
#end
#implementation ViewController {
UITableView *tableView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//UIbutton createtion
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button .backgroundColor = [UIColor orangecolor];
button.frame = CGRectMake(100, 440, 30, 30);
[self.view addSubview:button];
[self tablelistCreation];
}
//UItableView createtion
-(void)tablelistCreation{
tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, 300, self.420) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
//Delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];
label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
label.backgroundColor = [UIColor clearColor];
label.tag = indexPath.row;
if(!isTouchButton)
{
cell.labelUserName.backgroundColor = [UIColor clearColor];
}
else{
cell.labelUserName.backgroundColor = [UIColor redColor];
}
[cell.contentView addSubview:label];
return cell;
}
//button action
-(void)Method:(id)sender
{
if(!isTouchButton)
{
[tablelistCreation reloadData];
isTouchButton = YES;
}
else{
[tablelistCreation reloadData];
isTouchButton = NO;
}
}
I have a main view controller. It contains a table view controller ( in a container ) , i want to round out the tableview so it shows similarly to the facebook login one.
Code i have used in the child view controller ( which is a tableview controller ) in viewDidLoad :
self.tableView.layer.cornerRadius = 10.0f;
self.tableView.layer.masksToBounds = YES;
self.tableView.clipsToBounds = YES;
self.tableView.backgroundColor = [UIColor clearColor];
Result :
As you can see , when the field is selected , the corners SEEM rounded , but the white space remains. How can i make them rounded even when it isn't selected ?
Using : XCODE 5 , IOS 7
As you requested me to do, here is the code you wanted:
for (CALayer *subLayer in self.tableView.layer.sublayers)
{
subLayer.cornerRadius = 10;
subLayer.masksToBounds = YES;
}
Try this
#interface CustomtableViewController : UITableViewController<UITableViewDelegate, UITableViewDataSource>
{
UITextField * username;
UIButton * submit;
}
#implementation CustomtableViewController
- (void)viewDidLoad
{
UIView *newView = [[UIView alloc]initWithFrame:CGRectMake(10, 70, 300, 45)];
submit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[submit setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//[submit setTitleColor:[UIColor colorWithWhite:0.0 alpha:0.56] forState:UIControlStateDisabled];
[submit setTitle:#"Login" forState:UIControlStateNormal];
[submit.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
[submit setFrame:CGRectMake(10.0, 15.0, 280.0, 44.0)];
[newView addSubview:submit];
[self.tableView setTableFooterView:newView];
[super viewDidLoad];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//self.tableView.contentOffset = CGPointMake( 10, 320);
[self.tableView setContentInset:UIEdgeInsetsMake(50,0,0,0)];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([indexPath section] == 0) {
username = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
username.adjustsFontSizeToFitWidth = YES;
username.textColor = [UIColor blackColor];
if ([indexPath row] == 0) {
username.placeholder = #"example#gmail.com";
username.keyboardType = UIKeyboardTypeEmailAddress;
username.returnKeyType = UIReturnKeyNext;
cell.textLabel.text = #"Username";
username.clearButtonMode = YES;
}
else {
username.placeholder = #"minimum 6 characters";
username.keyboardType = UIKeyboardTypeDefault;
username.returnKeyType = UIReturnKeyDone;
username.secureTextEntry = YES;
cell.textLabel.text = #"Password";
username.clearButtonMode = UITextFieldViewModeAlways;
}
username.backgroundColor = [UIColor whiteColor];
username.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
username.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
username.textAlignment = NSTextAlignmentLeft;
username.tag = 0;
username.clearButtonMode = UITextFieldViewModeAlways; // no clear 'x' button to the right
[username setEnabled: YES];
[cell.contentView addSubview: username];
}
// Configure the cell...
return cell;
}
Here, i've created just two textfields for username and password. You can use the else if condition to insert any no of textfields in each of the successive rows according to your needs.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [NSString stringWithFormat:#"User Login"];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 50;
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
return #"";
}
So, my code here is just used for creating a login page with two textfields(Username and Password) and a Login button. You can modify my code according to your needs. Cheers!
I have made a form by code to sign up within the application but if I scroll up and down the cells which are not visible get overridden with the setup of other cells... Has anyone experienced something like that? I'm pretty sure that I'm missing out on some configuration but I don't know which.
Here is my code.
Its a lot of code....sorry. But I really don't know where to start
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 4;
} else if (section == 1) {
return 4;
} else {
return 5;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return #"Perönliche Informationen";
} else if (section == 1) {
return #"Anschrift";
} else {
return #"myClassico Profil";
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath section] == 0) {
if ([indexPath row] == 3) {
return 210;
} else {
return 44;
}
}
else if ([indexPath section] == 1) {
if ([indexPath row] == 0) {
return 80;
} else if ([indexPath row] == 3) {
return 210;
} else {
return 44;
}
}
else if ([indexPath section] == 2) {
return 44;
}
else {
return 44;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"anmeldenCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([indexPath section] == 0) {
if ([indexPath row] == 0) {
UISegmentedControl *gender = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Frau", #"Herr", nil]];
gender.frame = CGRectMake(99, 8, 124, 28);
gender.selectedSegmentIndex = 0;
[cell.contentView addSubview:gender];
} else if ([indexPath row] == 1) {
UITextField *vorname = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 300, 44)];
vorname.placeholder = #"Vorname";
[cell.contentView addSubview:vorname];
} else if ([indexPath row] == 2) {
UITextField *nachname = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 300, 44)];
nachname.placeholder = #"Nachname";
[cell.contentView addSubview:nachname];
} else {
//Code below is backup code in case someone really dislikes the UIDatePicker. Code is fully functional.
/*
UITextField *gebTag = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 80, 44)];
UITextField *gebMonat = [[UITextField alloc] initWithFrame:CGRectMake(108, 0, 100, 44)];
UITextField *gebJahr = [[UITextField alloc] initWithFrame:CGRectMake(216, 0, 100, 44)];
gebTag.placeholder = #"TT";
gebMonat.placeholder = #"MM";
gebJahr.placeholder = #"JJ";
[cell.contentView addSubview:gebTag];
[cell.contentView addSubview:gebMonat];
[cell.contentView addSubview:gebJahr];*/
UILabel *gebLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 280, 50)];
gebLabel.text = #"Geburtsdatum";
UIDatePicker *gender = [[UIDatePicker alloc] initWithFrame:CGRectMake(20, 30, 320, 180)];
[cell.contentView addSubview:gender];
[cell.contentView addSubview:gebLabel];
} //ENDE SECTION 1 (INFORMATIONEN ZUR PERSON)
} else if ([indexPath section] == 1) {
if ([indexPath row] == 0) {
UITextField *adresseStrasse = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
adresseStrasse.placeholder = #"Straße";
UITextField *adresseHNR = [[UITextField alloc] initWithFrame:CGRectMake(180, 30, 120, 44)];
adresseHNR.textAlignment = 2;
adresseHNR.placeholder = #"Hausnummer";
[cell.contentView addSubview:adresseStrasse];
[cell.contentView addSubview:adresseHNR];
} else if ([indexPath row] == 1) {
UITextField *adresseZusatz = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
adresseZusatz.placeholder = #"Adresszusatz (optional)";
[cell.contentView addSubview:adresseZusatz];
} else if ([indexPath row] == 2) {
UITextField *adressePLZ = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 100, 44)];
adressePLZ.placeholder = #"PLZ";
UITextField *adresseOrt = [[UITextField alloc] initWithFrame:CGRectMake(110, 0, 170, 44)];
adresseOrt.placeholder = #"Ort";
[cell.contentView addSubview:adressePLZ];
[cell.contentView addSubview:adresseOrt];
} else {
UILabel *land = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 150, 20)];
land.text = #"Land";
länderPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(20, 20, 300, 180)];
[länderPicker setDelegate:self];
array = [[NSMutableArray alloc] init];
[array addObject:#"Deutschland"];
[array addObject:#"Österreich"];
[array addObject:#"Schweiz"];
[cell.contentView addSubview:land];
[cell.contentView addSubview:länderPicker];
} //ENDE SECTION 2 (ADRESSEN INFORMATIONEN)
} else if ([indexPath section] == 2){
if ([indexPath row] == 0) {
UITextField *email = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
email.placeholder = #"E-Mail";
[cell.contentView addSubview:email];
} else if ([indexPath row] == 1) {
UITextField *telefon = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
telefon.placeholder = #"Telefon";
[cell.contentView addSubview:telefon];
} else if ([indexPath row] == 2) {
UITextField *mtelefon = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
mtelefon.placeholder = #"Mobiltelefon (optional)";
[cell.contentView addSubview:mtelefon];
} else if (indexPath.row == 3) {
UITextField *passwort = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
passwort.placeholder = #"Passwort";
[cell.contentView addSubview:passwort];
} else {
UITextField *passwortWiederholung = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, 280, 44)];
passwortWiederholung.placeholder = #"Passwort wiederholen";
[cell.contentView addSubview:passwortWiederholung];
} //ENDE SECTION 3 (MYCLASSICO ACCOUNT INFORMATIONEN)
}
return cell;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [array count];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [array objectAtIndex:row];
}
Use different identifier for cells:
static NSString *CellIdentifier = #"anmeldenCell";
or prepare reset function for them.
Your problem is that cells are reused between sections. You are adding subview here [cell.contentView addSubview:gender]; and you never remove it - so it will be added several times to the same cell + the cell with this view will be used in other sections as well.
Best approach will be to prepare custom cell per section and use different CellIdentifier for them. Also when you reuse them assing a view to it's properly instead of adding subview several times.
BTW. use enumerations for sections to increase readability, instead of 1,2,3,4 use kSectionMyProfile, etc...
When I scroll UITableView my tableview cells automatically uncheck. But when I press done button it give me selected rows I can't understand why is this happening.
My Tableview code is as below :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
idArray = [[NSMutableArray alloc]init];
NSLog(#"Cat Array : %#",catArray);
isSelectAllBtnClicked = NO;
serviceArray = [[NSArray alloc]initWithObjects:#"Anal", #"Cuffed", #"Foreplay", #"Masturbation", #"Scat", #"Blindfolded",#"Deep throat", #"French Kissing", #"Missionary", #"Shower for 2", #"Bottom", #"Dinner Date", #"Full Bondage", #"Mutual Masturbation", #"Spanking", #"Boy on Boy", #"Dirty Talk",#"Girl on Girl", #"On top", #"Strap on", #"Choking", #"Doggy", #"Girlfriend Experience", #"Oral", #"Striptease", #"CIM", #"Dominate", #"Golden Shower",#"Oral Mutual", #"Submissive", #"COB", #"Fantasy", #"Kissing", #"Overnight", #"Top", #"COF", #"Fetish", #"Light Bondage", #"Rim me",#"Touching", #"Couples", #"Fisting", #"Lingerie", #"Rim you", #"Toys for me", #"Cuddling", #"Foot fetish", #"Massage", #"Role Play", #"Toys for you", nil];
[myTableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseIdentifier = #"reuseIdentifier";
UITableViewCell *cell = [[UITableViewCell alloc]init];
if (cell != NULL)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
cell.selectionStyle = UITableViewCellEditingStyleNone;
cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"list_row_bg.png"]] autorelease];
UILabel *catListLbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 310, 20)];
NSString *strValue = [[NSUserDefaults standardUserDefaults]valueForKey:#"service"];
if ([strValue isEqualToString:#"service"])
{
catListLbl.text = [serviceArray objectAtIndex:indexPath.row];
topHaderLabel.text = #"Choose Services";
}
else
{
catListLbl.text = [catArray objectAtIndex:indexPath.row];
topHaderLabel.text = #"Choose Category";
}
catListLbl.textColor = [UIColor colorWithRed:244/255.0 green:29/255.0 blue:94/255.0 alpha:1.0];
catListLbl.backgroundColor = [UIColor clearColor];
[cell addSubview:catListLbl];
if (isSelectAllBtnClicked) {
UIButton *unCheckBtn = [[UIButton alloc]initWithFrame:CGRectMake(270, 10, 20, 20)];
[unCheckBtn setBackgroundImage:[UIImage imageNamed:#"checkbox_check.png"] forState:UIControlStateNormal];
unCheckBtn.tag = indexPath.row + 200;
//NSLog(#"%i",unCheckBtn.tag);
[cell addSubview:unCheckBtn];
[myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
[self tableView:myTableView didSelectRowAtIndexPath:indexPath];
}
else {
UIButton *unCheckBtn = [[UIButton alloc]initWithFrame:CGRectMake(270, 10, 20, 20)];
[unCheckBtn setBackgroundImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
unCheckBtn.tag = indexPath.row + 200;
//NSLog(#"%i",unCheckBtn.tag);
[cell addSubview:unCheckBtn];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int buttonTag = indexPath.row + 200;
//NSLog(#"%i",buttonTag);
UIButton *tempBtn = (UIButton *)[self.view viewWithTag:buttonTag];
[tempBtn setBackgroundImage:[UIImage imageNamed:#"checkbox_check.png"]forState:UIControlStateNormal];
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
int buttonTag = indexPath.row + 200;
NSLog(#"%i",buttonTag);
UIButton *tempBtn = (UIButton *)[self.view viewWithTag:buttonTag];
[tempBtn setBackgroundImage:[UIImage imageNamed:#"checkbox.png"]forState:UIControlStateNormal];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int count;
NSString *strValue = [[NSUserDefaults standardUserDefaults]valueForKey:#"service"];
if ([strValue isEqualToString:#"service"])
{
count = [serviceArray count];
}
else
{
count = [catArray count];
}
return count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
thanks in advance.
when ever you scroll the table
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method executes and you have not mentioned any condition by which it can be judged that this row was previously selected or not. you have to insert a condition along with indexPath.row and check whether it is prviously selected or not
try like this ,when you scroll the tableview every time isSelectAllBtnClicked value is 0 that's why every time button changed.
when you scroll the table every time new cell created try to avoid that one
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell.selectionStyle = UITableViewCellEditingStyleNone;
cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"list_row_bg.png"]] autorelease];
UILabel *catListLbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 310, 20)];
NSString *strValue = [[NSUserDefaults standardUserDefaults]valueForKey:#"service"];
if ([strValue isEqualToString:#"service"])
{
catListLbl.text = [serviceArray objectAtIndex:indexPath.row];
topHaderLabel.text = #"Choose Services";
}
else
{
catListLbl.text = [catArray objectAtIndex:indexPath.row];
topHaderLabel.text = #"Choose Category";
}
catListLbl.textColor = [UIColor colorWithRed:244/255.0 green:29/255.0 blue:94/255.0 alpha:1.0];
catListLbl.backgroundColor = [UIColor clearColor];
[cell addSubview:catListLbl];
if (isSelectAllBtnClicked) {
UIButton *unCheckBtn = [[UIButton alloc]initWithFrame:CGRectMake(270, 10, 20, 20)];
[unCheckBtn setBackgroundImage:[UIImage imageNamed:#"checkbox_check.png"] forState:UIControlStateNormal];
unCheckBtn.tag = indexPath.row + 200;
//NSLog(#"%i",unCheckBtn.tag);
[cell addSubview:unCheckBtn];
[myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
[self tableView:myTableView didSelectRowAtIndexPath:indexPath];
}
else {
UIButton *unCheckBtn = [[UIButton alloc]initWithFrame:CGRectMake(270, 10, 20, 20)];
[unCheckBtn setBackgroundImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
unCheckBtn.tag = indexPath.row + 200;
//NSLog(#"%i",unCheckBtn.tag);
[cell addSubview:unCheckBtn];
}
}
}