Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
710 views
in Technique[技术] by (71.8m points)

iphone - Incorrect number of objects getting added to mutable array

I have a view controller with table view that contains 8 cells(sections).I have created a textField and added that text field as a subview to 6 cells,remaining 2 cells of which one cell contains a text view and the other with a button.Because I am using the same text field,I have assigned tag values and then created a mutable array,added the textfield to that array.Here is the implementation code:

Taking in regard the suggestion given by Mr.Lanc,I have changed the implementation code

EDIT

- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

 NSString *identifier = @"UITableViewCell";

UITableViewCell *cell = (UITableViewCell *)[atableView dequeueReusableCellWithIdentifier:identifier];

    atableView.backgroundColor = [UIColor clearColor];

    textField = [[[UITextField alloc]initWithFrame:CGRectMake(15, 12, 300, 24)]autorelease];
    textField.textColor = [UIColor whiteColor];
    textField.delegate = self;
    tagValues = textField.tag;


switch (indexPath.section) 
{
    case 0:
    {
        NSString *cellIdentifierA = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
        if (cell == nil) 
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierA];
            cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"reminderbutton.png"]];
        }
        textField.placeholder = @"Enter name";
        textField.tag = 101;
        textField.text = reminderInstance.Name;
        textField.autocorrectionType = UITextAutocorrectionTypeNo;
        [cell.contentView addSubview:textField];
    }
        break;

    case 1:
    {
        NSString *cellIdentifierB = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
        if (cell == nil) 
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierB];
            cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"reminderbutton.png"]];
        }
        textField.tag = 102;
        textField.text = reminderInstance.Event;
        [cell.contentView addSubview:textField];
    }
        break;

    case 2:
    {
        NSString *cellIdentifierC = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
        if (cell == nil) 
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierC];
            cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"reminderbutton.png"]];
        }
        textField.placeholder = @"Click here to set date and time";
        textField.inputView = self.datePicker;
        textField.text = reminderInstance.Date;
        textField.tag = 103;
        [cell.contentView addSubview:textField];
    }   
        break;

    case 3:
    {
        NSString *cellIdentifierD = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
        if (cell == nil) 
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierD];
            cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"reminderbutton.png"]];
        }
        textField.tag = 105;
        textField.text = reminderInstance.numDays;
        textField.inputView = self.reminderPicker;
        [cell.contentView addSubview:textField];
    }
        break;

    case 4:
    {   
        NSString *cellIdentifierE = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
        if (cell == nil) 
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierE];
            cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"reminderbutton.png"]];
        }
        checkboxButton = [[[UIButton alloc] initWithFrame:CGRectMake(16,1,120, 44)]autorelease];
        [checkboxButton setImage:[UIImage imageNamed:@"ewee.png"] forState:UIControlStateNormal];
        [checkboxButton addTarget:self action:@selector(toggleButton:) forControlEvents:UIControlEventTouchUpInside];

        NSString *one = reminderInstance.selString;
        NSNumber* i = [NSNumber numberWithInt:[one intValue]];
        BOOL isOn = [i boolValue];

        if(isOn)
        {
            [checkboxButton setImage:[UIImage imageNamed:@"checkarrow.png"] forState:UIControlStateNormal];
        }
        else
        {
            [checkboxButton setImage:[UIImage imageNamed:@"ewee.png"] forState:UIControlStateNormal];
        }            

        [checkboxButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [checkboxButton setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)];
        [cell addSubview:checkboxButton];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(55, 10, 225, 24)];
        label.text = @"Every Year";
        label.textColor = [UIColor whiteColor];
        label.backgroundColor = [UIColor clearColor];
        [cell addSubview:label];
        cell.textLabel.textColor = [UIColor whiteColor];
        [label release];
    }
        break;

    case 5:
    {
        NSString *cellIdentifierF = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
        if (cell == nil) 
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierF];
            cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"reminderbutton.png"]];
        }
        textField.placeholder = @"Enter the number here";
        textField.text = num;
        textField.text = reminderInstance.number;
        textField.tag = 106; 
        textField.userInteractionEnabled = YES;
        textField.keyboardType = UIKeyboardTypeNumberPad;
        [cell.contentView addSubview:textField];
    }
        break;

    case 6:
    {
        NSString *cellIdentifierG = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
        if (cell == nil) 
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierG];
            cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"reminderbutton.png"]];
        }
        cell.backgroundColor = [UIColor clearColor];
        textView.clipsToBounds = YES;
        textView = [[UITextView alloc]initWithFrame: CGRectMake(-2, -3, 307, 154)];
        UIImageView *imgView = [[[UIImageView alloc]initWithFrame: textView.frame]autorelease];
        imgView.image = [UIImage imageNamed: @"reminderbuttonxl.png"];
        [textView addSubview: imgView];
        [textView sendSubviewToBack: imgView];
        textView.backgroundColor = [UIColor clearColor];
        textView.delegate = self;
        textView.tag = 11;
        textView.text = reminderInstance.msgBody;
        tagValues = textView.tag;
        textView.textColor = [UIColor whiteColor];
        [cell.contentView addSubview:textView];
        [textView release];
    }
        break;

    case 7:
    {
        NSString *cellIdentifierH = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
        if (cell == nil) 
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierH];
            cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"reminderbutton.png"]];
        }
        textField.tag = 107;
        textField.inputView = self.groupPicker;
        tagValues = textField.tag;
        textField.text = reminderInstance.remGroup;
        [cell.contentView addSubview:textField];
    }
        break;

    default:
        break;
}

    [self.fields addObject:textField];

NSLog(@"Fields = %@",fields);

return cell;

}

Now when I am trying to print the array using the following statement:

NSLog(@"Fields = %@",fields); I could observe the following in console window:

Fields = (
    "<UITextField: 0x9d043a0; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 101; layer = <CALayer: 0x9d044e0>>"
)
Fields = (
    "<UITextField: 0x9d043a0; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 101; layer = <CALayer: 0x9d044e0>>",
    "<UITextField: 0x9d07d50; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 102; layer = <CALayer: 0x9d07ca0>>"
)
Fields = (
    "<UITextField: 0x9d043a0; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 101; layer = <CALayer: 0x9d044e0>>",
    "<UITextField: 0x9d07d50; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 102; layer = <CALayer: 0x9d07ca0>>",
    "<UITextField: 0x7108650; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 103; layer = <CALayer: 0x7108620>>"
)
Fields = (
    "<UITextField: 0x9d043a0; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 101; layer = <CALayer: 0x9d044e0>>",
    "<UITextField: 0x9d07d50; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 102; layer = <CALayer: 0x9d07ca0>>",
    "<UITextField: 0x7108650; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 103; layer = <CALayer: 0x7108620>>",
    "<UITextField: 0x5c7bf90; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 105; layer = <CALayer: 0x5c7c920>>"
)
Fields = (
    "<UITextField: 0x9d043a0; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 101; layer = <CALayer: 0x9d044e0>>",
    "<UITextField: 0x9d07d50; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 102; layer = <CALayer: 0x9d07ca0>>",
    "<UITextField: 0x7108650; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 103; layer = <CALayer: 0x7108620>>",
    "<UITextField: 0x5c7bf90; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; tag = 105; layer = <CALayer: 0x5c7c920>>",
    "<UITextField: 0x9d08b20; frame = (15 12; 300 24); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x9d08af0>>"
)

Now as we can observe from the above o/p that number of fields that are getting printed is 5 i.e the 4 textfields and the cell with button,But as per our coding in -CellForRowAtIndexPath,6 cells contains textfield,hence the number of fields must be 6,but we are getting wrong result i.e. 5 (4 textfields+button) which is a blunder.

This is also leading me to saving the data in table view issue,i.e. for right navigation bar button item titled "Save" in my case,I have implemented:

-(IBAction)save:(id)sender
{
//Code for saving entered data using sqlite

    UITextField *fieldOne = [self.fields objectAtIndex:0];
    UITextField *fieldTwo = [self.fields objectAtIndex:1];
    UITextField *fieldThree = [self.fields objectAtIndex:2]; 
    UITextField *fieldFour = [self.fields objectAtIndex:3];
    UITextField *fieldFive = [self.fields objectAtIndex:5];
    UITextField *fieldSix = [self.fields objectAtIndex:7];
sqlite3_stmt *statement = nil;
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &remindersDB) == SQLITE_OK && textField.text != nil)
    {
if (self.navigationItem.rightBarButtonItem.title == @"Save")
        {

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Did you try scrolling the tableview? I

If you have added this code inside if( cell == nil ), then it may not create all the text fields, as the table cells are reused. Better you have different cell identifiers for all the 8 cells. That would solve your problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...