tags:

views:

123

answers:

2

HI all,

i need to create an dynamic array, every time i add object,it should not overwrite the first element.It should keep inserting into it.

eventually i need to insert that elements from array into table view. so from one view, user filling form first name,last name and hits enter. these two values should be inserted into array.Which is shown in table view,at the very first row.

second user comes, he fills firstname lastname and hits enter, this two values also should be inserted into array ,and in tableview,it should occupy first position and the previous values should shifted to 1 row below, i hope now i m more clear with my problem

regards shishir

+3  A: 

Check NSMutableArray.

NSMutableArray* arr = [[NSMutableArray alloc] init];
[arr addObject:@"FirstObject"];
[arr addObject:@"SecondObject"];
[arr addObject:@"ThirdObject"];
...
NSLog(@"%@", [arr objectAtIndex:0]); // prints "FirstObject".
...
[arr release];

Edit: There are 2 methods to insert an object into the front. The slow way is to use

[arr insertObject:@"NewObject" atIndex:0];

this is slow because every time an object is added, N items will be moved. If the part is not the performance bottleneck, this method is recommended as code depending on it can be written more easily.

The fast way is add to the end using -addObject:, but for row i, you get the object at [arr count] - i - 1.

KennyTM
@kenny,plz c my question gain,i edited it.
shishir.bobby
@shi: See update.
KennyTM
@kennywill this work?[appdelegate.completeDayArray insertObject:inSender atIndex:0];.................appdelegate.completeDayArray is my arrayinsertObject:inSender is the object i m trying to insert.everytime i insert new object,previous object in array will be there, right?
shishir.bobby
i dont knw y but everytime i insert object,it shows only 1 object..y this is happening.??
shishir.bobby
@shi: How did you implement your table data source?
KennyTM
hey kenny, i posted my code as a answer,plz have a look at it.thanks
shishir.bobby
A: 

i waz not able to write this in comment so,i pasede here.....

plz have a look

this is array

  • (NSURL *)prepareSMSUrlForPhoneNumber:(NSString *)inPhoeNumber sender:(NSString *)inSender message:(NSString *)inMessage { NSURL * url = nil;

    NSString *urlString = [NSString stringWithFormat:@"%@to=%@&from=%@&message=%@&sandbox=false&usr=%@&pwd=%@", kSendingURL,inPhoeNumber,inSender,inMessage,kUser,kPassword]; appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    appdelegate.completeToArray = [[NSMutableArray alloc]init];// initWithCapacity:20]; [appdelegate.completeToArray addObject:inSender ]; // [appdelegate.completeToArray insertObject:inSender atIndex:0]; // [appdelegate.completeToArray insertObject:@"NewObject" atIndex:0];

// [appdelegate.completeToArray writeToFile:filePath atomically:YES];

appdelegate.completeFromArray = [[NSMutableArray alloc]init];// initWithCapacity:20];
[appdelegate.completeFromArray addObject:inPhoeNumber];
//[appdelegate.completeFromArray insertObject:inPhoeNumber atIndex:0];



appdelegate.completeMessageArray = [[NSMutableArray alloc]init];// initWithCapacity:20];
[appdelegate.completeMessageArray addObject:inMessage];
//[appdelegate.completeMessageArray insertObject:inMessage atIndex:0];  

// [appdelegate.completeMessageArray insertObject:inSender atIndex:1];

///////////////////////////////////
//if (appdelegate.completeToArray == nil) {

// appdelegate.completeToArray = [[NSMutableArray alloc] initWithCapacity:20]; // [appdelegate.completeToArray addObject:inSender]; // [defaults setObject:appdelegate.completeToArray forKey:@"toKey"]; // [appdelegate.completeToArray release]; // }
///////////////////////////////////

/////////////
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"hh:mm aaa dd.MMMM.yyyy"];

NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm:ss"];

NSDate *now = [[NSDate alloc] init];

NSString *theDate = [dateFormat stringFromDate:now];
NSString *theTime = [timeFormat stringFromDate:now];

NSLog(@"\n"

      "theDate: |%@| \n"
      "theTime: |%@| \n"
      , theDate, theTime);

[dateFormat release];
[timeFormat release];
[now release];


    appdelegate.completeDayArray = [[NSMutableArray alloc] init];
    [appdelegate.completeDayArray addObject:theDate];

// [appdelegate.completeDayArray insertObject:theDate atIndex:0];

appdelegate.completeTimeArray= [[NSMutableArray alloc] init];
[appdelegate.completeTimeArray addObject:theTime];

// [appdelegate.completeTimeArray insertObject:theTime atIndex:0];

/////////////


url = [ [NSURL alloc] initWithString:urlString];
return [url autorelease];

} //////////////////////////////////////

and this is table

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }

// Customize the number of rows in the table view. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { //int count = [arrayTemaplate count]; //int countsentSms = [arraySentSms count];

//if(self.editing) count++;
//return count;

return 1; //[arraySentSms count];

}

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //////////////////////////// customize cell//////////////// static NSString *CellIdentifier2 = @"Cell2"; static NSInteger toTag = 1; static NSInteger fromTag = 2; static NSInteger messageTag = 3;

    static NSInteger timeTag = 5;

    UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; if (cell2 == nil) {

    cell2 = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier2] autorelease];
            cell2.textLabel.textColor = [UIColor whiteColor];
    CGRect frame;
    
    
    frame.origin.x = 5; 
    frame.origin.y = 3;
    frame.size.height = 15;
    frame.size.width = 300;
    
    
    UILabel *fromLabel = [[UILabel alloc] initWithFrame:frame];
    fromLabel.font = [UIFont boldSystemFontOfSize:12.0];
    fromLabel.textColor = [UIColor whiteColor];
    fromLabel.backgroundColor = [UIColor blackColor];
    fromLabel.tag = fromTag;
    [cell2.contentView addSubview:fromLabel];
    [fromLabel release];
    
    
    //frame.origin.x += 180;
    

    // frame.origin.y = 10; // UILabel *dayLabel = [[UILabel alloc] initWithFrame:frame]; // dayLabel.font = [UIFont boldSystemFontOfSize:12.0]; // dayLabel.textColor = [UIColor blueColor]; // dayLabel.backgroundColor = [UIColor blackColor];
    // dayLabel.tag = dayTag; // [cell2.contentView addSubview:dayLabel]; // [dayLabel release];

    frame.origin.x += 170;
    frame.origin.y = 6;
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:frame];
    timeLabel.font = [UIFont boldSystemFontOfSize:12.0];
    timeLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
    timeLabel.textColor = [UIColor blueColor];
    timeLabel.backgroundColor = [UIColor blackColor];       
    timeLabel.tag = timeTag;
    [cell2.contentView addSubview:timeLabel];
    [timeLabel release];
    
    
    frame.origin.x = 5; 
    frame.origin.y = 18;
    UILabel *toLabel = [[UILabel alloc] initWithFrame:frame];
    toLabel.font = [UIFont boldSystemFontOfSize:12.0];
    //toLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    toLabel.textColor = [UIColor whiteColor];
    toLabel.backgroundColor = [UIColor blackColor];     
    toLabel.tag = toTag;
    [cell2.contentView addSubview:toLabel];
    [toLabel release];
    
    
    frame.origin.x = 5;
    frame.origin.y = 30;
    UILabel *messageLabel = [[UILabel alloc] initWithFrame:frame];
    messageLabel.font = [UIFont boldSystemFontOfSize:12.0];
    //messageLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    messageLabel.textColor = [UIColor whiteColor];
    messageLabel.backgroundColor = [UIColor blackColor];        
    messageLabel.tag = messageTag;
    [cell2.contentView addSubview:messageLabel];
    [messageLabel release];
    

    }

    UILabel *fromLabel = (UILabel *) [cell2.contentView viewWithTag:fromTag]; UILabel *toLabel = (UILabel *) [cell2.contentView viewWithTag:toTag]; UILabel *messageLabel = (UILabel *) [cell2.contentView viewWithTag:messageTag]; UILabel *timeLabel = (UILabel *) [cell2.contentView viewWithTag:timeTag];

    /// // int i = 0; // for (NSArray *count in appdelegate.completeToArray) { // [appdelegate.completeToArray addObject:[NSIndexPath indexPathForRow:i++ inSection:0]]; // i++ // }

    //// appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; fromLabel.font = [UIFont boldSystemFontOfSize:14.0f]; toLabel.font = [UIFont boldSystemFontOfSize:14.0f]; messageLabel.font = [UIFont boldSystemFontOfSize:10.0f]; fromLabel.text = [NSString stringWithFormat:@"To : %@",[appdelegate.completeFromArray objectAtIndex:indexPath.row]] ; toLabel.text = [NSString stringWithFormat:@"From : %@",[appdelegate.completeToArray objectAtIndex:indexPath.row]]; messageLabel.text = [appdelegate.completeMessageArray objectAtIndex:indexPath.row]; timeLabel.text = [appdelegate.completeDayArray objectAtIndex:indexPath.row];
    return cell2;

    /////////////////////customize cell////////// static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; cell.hidesAccessoryWhenEditing = YES; } int count = 0; if(self.editing && indexPath.row != 0) count = 1;

    NSLog([NSString stringWithFormat:@"%i,%i",indexPath.row,(indexPath.row-count)]); // Set up the cell... if(indexPath.row == ([arraySentSms count]) && self.editing){ //cell.text = @"add new row"; return cell; }

    cell.text = [arraySentSms objectAtIndex:indexPath.row];

    return cell;

}

shishir.bobby