Its hard to explain to images will help. When the UITableView first loads, the detailText is above the textLabel - as such:
But when you move the tableView around a bit, it corrects itself, as such:
Here is my code for adjusting for everything I feel is applicable:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
cell.detailTextLabel.numberOfLines = 0;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
if (indexPath.row == 0){
cell.textLabel.text = #"Two";
cell.detailTextLabel.text = [self.rules objectForKey:#"two"];
}
if (indexPath.row == 1){
cell.textLabel.text = #"Three";
}
if (indexPath.row == 2){
cell.textLabel.text = #"Four";
}
if (indexPath.row == 3){
cell.textLabel.text = #"Five";
}
if (indexPath.row == 4){
cell.textLabel.text = #"Six";
}
if (indexPath.row == 5){
cell.textLabel.text = #"Seven";
}
if (indexPath.row == 6){
cell.textLabel.text = #"Eight";
}
if (indexPath.row == 7){
cell.textLabel.text = #"Nine";
}
if (indexPath.row == 8){
cell.textLabel.text = #"Ten";
}
if (indexPath.row == 9){
cell.textLabel.text = #"Jack";
}
if (indexPath.row == 10){
cell.textLabel.text = #"Queen";
}
if (indexPath.row == 11){
cell.textLabel.text = #"King";
}
if (indexPath.row == 12){
cell.textLabel.text = #"Ace";
}
CGRect frame = tableView.frame;
UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-70, 10, 50, 30)];
[addButton setTitle:#"Get" forState:UIControlStateNormal];
addButton.titleLabel.font = [UIFont fontWithName:#"Avenir-Black" size:14.0f];
addButton.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:addButton];
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
[cell.detailTextLabel sizeToFit];
return cell;
}
The row height:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *str = [self.rules objectForKey:#"two"];
CGSize size = [str sizeWithFont:[UIFont fontWithName:#"Avenir" size:17] constrainedToSize:CGSizeMake(280, 999) lineBreakMode:NSLineBreakByWordWrapping];
return size.height + 40;
}
Related
Im trying to add parse data into my table array and i get'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0] any help on how to resolve this would be appreciated.
PFQuery *query31 = [PFQuery queryWithClassName:#"Salesman"];
[query31 whereKey:#"SalesNo" equalTo:self.tbl22];
// query31.cachePolicy = kPFCachePolicyCacheThenNetwork;
[query31 getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!object) {
NSLog(#"The getFirstObject request failed.");
} else {
self.salesman = [object objectForKey:#"Salesman"];
NSLog(#"salesStr is %#",self.salesman);
}
}];
tableData2 = [NSArray arrayWithObjects:t21, t22, t23, t24, t25, nil];
here is were i get the crash t22 = self salesman
if ([_formController isEqual: #"Leads"]) {
if ( ( ![t22 isEqual:[NSNull null]] ) && ( [self.tbl22 length] != 0 ) )
{t22 = self.salesman;
} else { t22 = #"None"; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([tableView isEqual:self.listTableView]) {
return [tableData count];
} else if ([tableView isEqual:self.listTableView2]) {
return [tableData count];
} else if ([tableView isEqual:self.newsTableView]) {
return 1;
}
return 0;
}
#pragma mark TableView Delegate Methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([tableView isEqual:self.listTableView]) {
static NSString *CellIdentifier = #"NewCell";
UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (myCell == nil) {
myCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; }
//need to reload table (void)viewDidAppear to get fonts to change but its annoying
myCell.textLabel.text = [tableData4 objectAtIndex:indexPath.row];
myCell.textLabel.font = [UIFont systemFontOfSize:8.0];
[myCell.textLabel setTextColor:[UIColor darkGrayColor]];
myCell.detailTextLabel.text = [tableData objectAtIndex:indexPath.row];
myCell.detailTextLabel.font = [UIFont boldSystemFontOfSize:8.0];
[myCell.detailTextLabel setTextColor:[UIColor blackColor]];
return myCell;
}
else if ([tableView isEqual:self.listTableView2]) {
static NSString *CellIdentifier2 = #"NewCell2";
UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (myCell == nil) {
myCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]; }
//need to reload table (void)viewDidAppear to get fonts to change but its annoying
myCell.textLabel.text = [tableData3 objectAtIndex:indexPath.row];
myCell.textLabel.font = [UIFont systemFontOfSize:8.0];
[myCell.textLabel setTextColor:[UIColor darkGrayColor]];
myCell.detailTextLabel.text = [tableData2 objectAtIndex:indexPath.row];
myCell.detailTextLabel.font = [UIFont boldSystemFontOfSize:8.0];
[myCell.detailTextLabel setTextColor:[UIColor blackColor]];
//draw red vertical line
UIView *vertLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, myCell.frame.size.height)];
vertLine.backgroundColor = [UIColor redColor];
[myCell.contentView addSubview:vertLine];
return myCell;
}
else if ([tableView isEqual:self.newsTableView]) {
static NSString *CellIdentifier1 = #"detailCell";
CustomTableViewCell *myCell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
if (myCell == nil) {
myCell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1]; }
//need to reload table (void)viewDidAppear to get fonts to change but its annoying
myCell.separatorInset = UIEdgeInsetsMake(0.0f, myCell.frame.size.width, 0.0f, 400.0f);
myCell.leadtitleLabel.text = self.lnewsTitle;
myCell.leadtitleLabel.numberOfLines = 0;
myCell.leadtitleLabel.font = [UIFont systemFontOfSize:12.0];
[myCell.leadtitleLabel setTextColor:[UIColor darkGrayColor]];
myCell.leadsubtitleLabel.text = #"Yahoo Finance 2 hrs ago";
myCell.leadsubtitleLabel.font = [UIFont systemFontOfSize:8.0];
[myCell.leadsubtitleLabel setTextColor:[UIColor grayColor]];
myCell.leadreadmore.text = #"Read more";
myCell.leadreadmore.font = [UIFont systemFontOfSize:8.0];
myCell.leadnews.text = comments;
myCell.leadnews.numberOfLines = 0;
myCell.leadnews.font = [UIFont boldSystemFontOfSize:8.0];
[myCell.leadnews setTextColor:[UIColor darkGrayColor]];
return myCell;
}
when i run log i get one result Salesman name
NSLog(#"salesStr is %#",self.salesman);
salesStr is Adam Monteleone
It means that you array is empty and you try to access the index 1 which is not able to get it. You better debug yourself.
And if you want to do the introspection on the NSString, you only need to use the length method to check. It will return false if your NSString is either nil or empty.
I am trying to create tableview with UISwitch for some specific rows and AccessoryDisclosureIndicator for rest of cells.
I get the desired result as one check in following code.But the issue is , when i scroll the table view abruptly then the position of switch changes .. Which is undesired how can i get rid of this.
Please do reply to this thread
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
int _fontSize = fontSize;
NSArray *switchSettings = [[NSUserDefaults standardUserDefaults] arrayForKey:#"switchGeneralSettings"];
NSLog(#"switch settings :%#",switchSettings);
static NSString *cellIdentifier = #"generalSettingsTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSLog(#"*******************1*************");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
// code to additionally configure cell for multiple other UI components..
if(indexPath.row == 3 || indexPath.row == 4 || indexPath.row == 5 || indexPath.row == 6 || indexPath.row == 7)
{
NSLog(#"switch*******************%d*************",indexPath.row);
switchObj = [[UISwitch alloc] initWithFrame:CGRectZero];
switchObj.tag = indexPath.row + 20002;
cell.accessoryView = switchObj;
[switchObj addTarget: self action: #selector(flip:) forControlEvents:UIControlEventValueChanged];
[switchObj release];
}
else
{
NSLog(#"indicator*******************%d*************",indexPath.row);
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
else
{
if(indexPath.row == 3 || indexPath.row == 4 || indexPath.row == 5 || indexPath.row == 6 || indexPath.row == 7)
{
NSLog(#"*******************2*************");
countSwitch = 0;
switchObj = (UISwitch *)[cell.contentView viewWithTag:indexPath.row + 20002];
// switch
[switchObj setOn:NO];
if ([switchSettings count]>0) {
NSString *settingState = [switchSettings objectAtIndex:countSwitch];
if ([settingState isEqualToString:#"ON"]) {
[switchObj setOn:YES];
}
}
countSwitch++;
}
}
// confiure the cell here...
NSLog(#"*******************3*************");
NSString *cellTextLabel = [listGeneralSettings objectAtIndex:indexPath.row];
cell.textLabel.text = cellTextLabel;
cell.textLabel.font = [UIFont systemFontOfSize:_fontSize];
cell.detailTextLabel.text = [listDetailGeneralSettings objectAtIndex:indexPath.row];
NSLog(#" cell.detailTextLabel.text :%#", cell.detailTextLabel.text);
NSLog(#"*******************end*************");
return cell;
}
Here cellone is tableviewCell, in that cell you can add uiswitch
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath*)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
// ... some text label stuff deleted here.
cell.opaque = NO;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
[cell setBackgroundColor: [UIColor colorWithPatternImage:[UIImage imageNamed:#"row_2_blank.png"]]];
// [cell prepareForReuse];
}
if (indexPath.row == 0)
{
selectAllB.tag=100;
[selectAllB addTarget:self action:#selector(click:)forControlEvents:UIControlEventTouchUpInside];
[cellOne setBackgroundColor: [UIColor colorWithPatternImage:[UIImage imageNamed:#"row_2_blank.png"]]];
cellOne.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cellOne;
}
return cell;
}
likewise you can use many tableview cell inwhich you can add ui elements accordingly
You're only updating the cell when it's first created. You need to update it every time. Try it like this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
int _fontSize = fontSize;
NSArray *switchSettings = [[NSUserDefaults standardUserDefaults] arrayForKey:#"switchGeneralSettings"];
NSLog(#"switch settings :%#",switchSettings);
static NSString *cellIdentifier = #"generalSettingsTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSLog(#"*******************1*************");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
// code to additionally configure cell for multiple other UI components..
// configure the cell here...
if(indexPath.row == 3 || indexPath.row == 4 || indexPath.row == 5 || indexPath.row == 6 || indexPath.row == 7)
{
NSLog(#"switch*******************%d*************",indexPath.row);
switchObj = [[UISwitch alloc] initWithFrame:CGRectZero];
switchObj.tag = indexPath.row + 20002;
cell.accessoryView = switchObj;
[switchObj addTarget: self action: #selector(flip:) forControlEvents:UIControlEventValueChanged];
[switchObj release];
}
else
{
NSLog(#"indicator*******************%d*************",indexPath.row);
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if(indexPath.row == 3 || indexPath.row == 4 || indexPath.row == 5 || indexPath.row == 6 || indexPath.row == 7)
{
NSLog(#"*******************2*************");
countSwitch = 0;
switchObj = (UISwitch *)[cell.contentView viewWithTag:indexPath.row + 20002];
// switch
[switchObj setOn:NO];
if ([switchSettings count]>0) {
NSString *settingState = [switchSettings objectAtIndex:countSwitch];
if ([settingState isEqualToString:#"ON"]) {
[switchObj setOn:YES];
}
}
countSwitch++;
}
NSLog(#"*******************3*************");
NSString *cellTextLabel = [listGeneralSettings objectAtIndex:indexPath.row];
cell.textLabel.text = cellTextLabel;
cell.textLabel.font = [UIFont systemFontOfSize:_fontSize];
cell.detailTextLabel.text = [listDetailGeneralSettings objectAtIndex:indexPath.row];
NSLog(#" cell.detailTextLabel.text :%#", cell.detailTextLabel.text);
NSLog(#"*******************end*************");
return cell;
}
I created at least 10 cells with each UITextField on it, for registration page. When I insert the words on the textfield in the first cell, and when I scrolled the tableView, the textfield which is in another cell show up the word that I just typed.
Below is my code. The textField data which I input is looping, which is caused by dequeueReusableCellWithIdentifier... How can I solve this problem? Thank you very much.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"RCell";
RegisterCell *cell = (RegisterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[RegisterCell alloc] initWithFrame:CGRectMake(0, 0, 280, 44) reuseIdentifier:CellIdentifier] autorelease];
if(indexPath.section == 0){
NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]];
NSString *mainLabel = [NSString stringWithFormat:#"%#", [rowData objectForKey:#"regisLbl"]];
cell.registerLabel.text = mainLabel;
UITextField *valTxtField = [[UITextField alloc] initWithFrame:CGRectMake(120, 5, 180, 30)];
valTxtField.font = [UIFont fontWithName:#"Futura-CondensedExtraBold" size:18.0];
valTxtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
valTxtField.delegate = self;
valTxtField.returnKeyType = UIReturnKeyDone;
valTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
valTxtField.autocapitalizationType = UITextAutocapitalizationTypeNone;
if(indexPath.row == 0)
{
valTxtField.text = #"";
emailTxtFld = valTxtField; //emailTxtFld is global variable
}
if(indexPath.row == 1)
{
valTxtField.text = #"";
reEmailTxtFld = valTxtField; //reEmailTxtFld is global variable
}
[cell.contentView addSubview:valTxtField];
[valTxtField release];
}
else if(indexPath.section == 1){
NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]+10];
NSString *mainLabel = [NSString stringWithFormat:#"%#", [rowData objectForKey:#"regisLbl"]];
cell.registerLabel.text = mainLabel;
cell.registerTextField.enabled = NO;
}
else if(indexPath.section == 2){
if(indexPath.row == 0){
NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]+11];
NSString *mainLabel = [NSString stringWithFormat:#"%#", [rowData objectForKey:#"regisLbl"]];
cell.registerLabel.text = mainLabel;
cell.registerTextField.enabled = NO;
}
}
return cell;
}
An easy way is to just remove all subviews from the cells contentView before you add subviews, example:
for (UIView *subview in [cell.contentView subviews])
[subview removeFromSuperview];
A more efficient way would be to do all the creation of cells inside the if (cell == nil) statement, but that depends on how many cells you have in the table.
The right implementation is to move the creation of any view whithin if (cell == nil)
Like so:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"RCell";
RegisterCell *cell = (RegisterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITextField *valTxtField;
if (cell == nil)
{
cell = [[[RegisterCell alloc] initWithFrame:CGRectMake(0, 0, 280, 44) reuseIdentifier:CellIdentifier] autorelease];
if(indexPath.section == 0){
valTxtField = [[UITextField alloc] initWithFrame:CGRectMake(120, 5, 180, 30)];
valTxtField.font = [UIFont fontWithName:#"Futura-CondensedExtraBold" size:18.0];
valTxtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
valTxtField.delegate = self;
valTxtField.returnKeyType = UIReturnKeyDone;
valTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
valTxtField.autocapitalizationType = UITextAutocapitalizationTypeNone;
valTxtField.tag = 100;
[cell.contentView addSubview:valTxtField];
[valTxtField release];
}
}
valTxtField = (UITextField *)[cell.contentView viewWithTag:100];
if(indexPath.section == 0){
NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]];
NSString *mainLabel = [NSString stringWithFormat:#"%#", [rowData objectForKey:#"regisLbl"]];
cell.registerLabel.text = mainLabel;
if(indexPath.row == 0)
{
valTxtField.text = #"";
emailTxtFld = valTxtField; //emailTxtFld is global variable
}
if(indexPath.row == 1)
{
valTxtField.text = #"";
reEmailTxtFld = valTxtField; //reEmailTxtFld is global variable
}
}
else if(indexPath.section == 1){
NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]+10];
NSString *mainLabel = [NSString stringWithFormat:#"%#", [rowData objectForKey:#"regisLbl"]];
cell.registerLabel.text = mainLabel;
cell.registerTextField.enabled = NO;
}
else if(indexPath.section == 2){
if(indexPath.row == 0){
NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]+11];
NSString *mainLabel = [NSString stringWithFormat:#"%#", [rowData objectForKey:#"regisLbl"]];
cell.registerLabel.text = mainLabel;
cell.registerTextField.enabled = NO;
}
}
return cell;
I have a problem on uitableview,
I have taken 3 sections in grouped table,
Inserted data for each section by using indexPath.section all is well but 3rd section is filled with both 1st and 2nd section data,
How to remove that data and how to fill my own data means separate data?
Code is:-
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return #"Product Details";
}
if (section == 1) {
return #"Ingredients";
}
if (section == 2) {
return #"My Allergies";
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
return 150;
}
if (indexPath.section == 1) {
return 100;
}
if (indexPath.section==2) {
return 45;
}
return 0;
}
- (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];
}
NSMutableString *redStr1 =[[NSMutableString alloc] initWithString:#"No" ];
NSMutableString *yellowStr1 =[[NSMutableString alloc] initWithString:#"No" ];
NSMutableString *greenStr1 =[[NSMutableString alloc] initWithString:#"No" ];
int a = [appDelegate.reIndex intValue];
NSDictionary *aDict1 = [[NSDictionary alloc]init];
aDict1 = [appDelegate.ProductArray objectAtIndex:a];
// NSMutableString *str = [aDict1 objectForKey:#"IngredientInfo"];
NSMutableArray *array =[aDict1 objectForKey:#"IngredientInfo1"];
NSMutableString *nameStr = [[NSMutableString alloc] init];
NSMutableString *nameClr = [[NSMutableString alloc] init];
for (int s=0; s<[array count]; s++) {
NSDictionary *nameDict = [array objectAtIndex:s];
[nameStr appendString:[nameDict objectForKey:#"Name"]];
nameClr = [nameDict objectForKey:#"HalaStatus"];
if ([nameClr isEqualToString:#"Red"]) {
[redStr1 setString:#"Yes"];
}
if ([nameClr isEqualToString:#"Yellow"]) {
[yellowStr1 setString:#"Yes"];
}
if ([nameClr isEqualToString:#"Green"]) {
[greenStr1 setString:#"Yes"];
}
if (s == [array count]-1) {
[nameStr appendFormat:#"."];
}
else {
[nameStr appendFormat:#","];
}
}
if (indexPath.section == 0)
{
cell.userInteractionEnabled =NO;
imgview1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"images (1).jpg"] ];
[imgview1 setFrame:CGRectMake(12, 2, 100, 145)];
[cell addSubview:imgview1];
[imgview1 release];
imgview = [[UIImageView alloc]initWithFrame:CGRectMake(255,2 , 50, 45)];
[cell addSubview:imgview];
if ([redStr1 isEqualToString:#"Yes"]) {
[imgview setImage:[UIImage imageNamed:#"Red.png"]];
}
if ([redStr1 isEqualToString:#"No"] && [yellowStr1 isEqualToString:#"Yes"] ) {
[imgview setImage:[UIImage imageNamed:#"Yellow.png"]];
}
if ([redStr1 isEqualToString:#"No"] && [yellowStr1 isEqualToString:#"No"] && [greenStr1 isEqualToString:#"Yes"]) {
[imgview setImage:[UIImage imageNamed:#"Green.png"]];
}
}
if (indexPath.section == 1) {
UITextView *textview1;
textview1 =[[UITextView alloc]initWithFrame:CGRectMake(12, 2, 294, 96)];
textview1.text = nameStr;
textview1.editable =NO;
[textview1 setFont:[UIFont systemFontOfSize:15]];
[cell addSubview:textview1];
}
if (indexPath.section == 2) {
cell.textLabel.text = [arr objectAtIndex:indexPath.row];
}
return cell;
}
if you have an array and access this in cellForRowAtIndexPath: with indexPath.row you will get for all sections the same lets say three elements.
What you have to do is an array inside an array:
Level 1 Section 1
Level 2 Row 1
Level 2 Row 2
Level 2 Row 3
Level 1 Section 2
Level 2 Row 1
Level 2 Row 2
Level 1 Section 3
Level 2 Row 1
Level 2 Row 2
Level 2 Row 3
I have 5 seperate sections in a table, 2 of the sections have customizable cells, when in EDITING mode, 2 of the sections produce an additional cell that will add a new cell to the list. My problem is that when I invoke edit mode, the UISwitch glitches! and decides to jump around, VERY SIMPLE code, just have NO IDEA why the UISwitch is jumping to different cells in DIFFERENT SECTIONS when it should NOT!
static NSInteger kCustomTextTag = 1;
static NSInteger kServiceTag = 3;
static NSInteger kConnectTag = 4;
static NSInteger kVibrateTag = 1;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Profile_ManagerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.hidesAccessoryWhenEditing = YES;
}
UISwitch *swService = nil;
swService = [[[UISwitch alloc] initWithFrame:CGRectMake(195, 8, 160, 27)] autorelease];
[swService addTarget:self action:#selector(serviceSwAction:) forControlEvents:UIControlEventValueChanged];
swService.tag = kServiceTag;
UISwitch *swConnect = nil;
swConnect = [[[UISwitch alloc] initWithFrame:CGRectMake(195, 8, 160, 27)] autorelease];
[swConnect addTarget:self action:#selector(connectSwAction:) forControlEvents:UIControlEventValueChanged];
swConnect.tag = kConnectTag;
UISwitch *swVibrate = nil;
swVibrate = [[[UISwitch alloc] initWithFrame:CGRectMake(195, 8, 160, 27)] autorelease];
[swVibrate addTarget:self action:#selector(vibrateSwAction:) forControlEvents:UIControlEventValueChanged];
swVibrate.tag = kVibrateTag;
if(indexPath.section == 0)
{
//CGRectMake Method (x, y, width, height)
custProfileNameLabel = [[[UITextField alloc] initWithFrame:CGRectMake(10, 11, 290, 21)] autorelease];
custProfileNameLabel.tag = kCustomTextTag;
custProfileNameLabel.textAlignment = UITextAlignmentLeft;
[cell.contentView addSubview:custProfileNameLabel];
custProfileNameLabel.backgroundColor = [UIColor clearColor];
custProfileNameLabel.userInteractionEnabled = NO;
custProfileNameLabel.placeholder = #"Custom Profile Name";
custProfileNameLabel.autocapitalizationType = UITextAutocapitalizationTypeWords;
custProfileNameLabel.clearButtonMode = UITextFieldViewModeWhileEditing;
//UIKeyboardTypeDefault;
cell.selectionStyle = UITableViewCellEditingStyleNone;
CustomProfile *cProf = [CustomProfile alloc];
cell.textLabel.text = [cProf tName];
}
if(indexPath.section == 1)
{
if(indexPath.row == ([[appDelegate serviceArray] count]) && self.editing){
cell.textLabel.text = #"Add a Service";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
swService.hidden = YES;
return cell;
}else{
swService.hidden = NO;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = [[appDelegate serviceArray] objectAtIndex:indexPath.row];
}
[cell addSubview:swService];
}
if(indexPath.section == 2)
{
if(indexPath.row == ([[appDelegate connectArray] count]) && self.editing){
cell.textLabel.text = #"Add a Connection";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
swConnect.hidden = YES;
return cell;
}else{
swConnect.hidden = NO;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = [[appDelegate connectArray] objectAtIndex:indexPath.row];
}
[cell addSubview:swConnect];
}
if(indexPath.section == 3)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPlistPath = [documentsDirectory stringByAppendingPathComponent:#"ProfileManager.plist"];
NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:myPlistPath];
NSString *value;
value = [plistDict objectForKey:#"Custom Ringtone"];
if(indexPath.row == ([[appDelegate ringArray] count]) && self.editing){
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
}else{
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = value;
}
if(indexPath.section == 4)
{
if(indexPath.row == 0 && self.editing)
{
//cell.userInteractionEnabled = NO;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = #"Vibrate";
swVibrate.hidden = YES;
}else{
swVibrate.hidden = NO;
//cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.text = #"Vibrate";
}
[cell.contentView addSubview:swVibrate];
}
return cell;
}
Screenshot
Well, the reason this glitch was made possible is because the Controls were created when creating the cell, so it just continued to create additional controls when view appeared.
If you are receiving this glitch too, make sure you create the controls at ViewDidLoad, or call a void, just dont create the controls in the cellForRowAtIndexPath:
Nothing freaky is going on. This is normal behavior. Note the line UITableViewCell *cell = dequeueReusableCellWithIdentifier ... The tableview controller keeps a queue of previously used cells for efficiency purposes, and when you scroll a new section of the tableview into view, it may return a previously used cell for you to reuse (in which case the value of the returned cell would not be nil). If that used cell originally had a uiswitch embedded in it, you'll get that switch back again, even if the current cell doesn't use it.
You need to check in your cell allocation code whether the cell being given back to you is an existing one or not, and remove the switch if it's present and you don't want it.
Howard