Jignesh Fadadu
Jignesh Fadadu

Reputation: 409

How to store data in the tableView & update it next time

-(IBAction)btnSaveScore:(id)sender
{
   if(!dictWinData)
      dictWinData = [[NSMutableDictionary alloc] init];


    array = [NSMutableArray arrayWithObjects:txt_EnterName.text,
                                             [NSString stringWithFormat:@"%i",iTap], nil];

    int increment = 0;

    NSLog(@"array data is:--> %@",array);

    for (int intWinData = 1; intWinData < [array count]; intWinData++)
    {
        [dictWinData setObject:array forKey:[NSString stringWithFormat:@"NameScore%d",increment]];
        increment++;
    }
}

This is the code to add my score of the game with name.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dictWinData count];
}

- (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];

        ArrStr = [dictWinData valueForKey:[NSString stringWithFormat:@"NameScore%d",indexPath.row]];
        UILabel *lblDayName = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 160, 21)];
        lblDayName.text = [ArrStr objectAtIndex:0];
        lblDayName.textColor = [UIColor blackColor];
        lblDayName.font = [UIFont boldSystemFontOfSize:17.0];
        lblDayName.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:lblDayName];
        [lblDayName release];

        UILabel *lblLow = [[UILabel alloc] initWithFrame:CGRectMake(180, 50, 40, 21)];
        lblLow.text = [ArrStr objectAtIndex:1];
        lblLow.textColor = [UIColor blackColor];
        lblLow.font = [UIFont boldSystemFontOfSize:17.0];
        lblLow.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:lblLow];
        [lblLow release];
    }
return cell;
}

This is storing name & score of the player but each time it displays only one record in the table.

It must show the last top ten scores in the list.

Main problem is that this is not storing & showing every score data.

Please guide me to solve this problem.

Thanks in advance.

Upvotes: 0

Views: 133

Answers (2)

TARAK PAREKH
TARAK PAREKH

Reputation: 61

You need to give the tag of each and every control that you want to display in UITableView and outside the cell==nil condition you need to retrieve that control with the specific tag and then give the text assignment to it.

Upvotes: 2

Midhun MP
Midhun MP

Reputation: 107231

You cannot use table view for storing data. Table view is an UI Element which can be used for display your data. UITableView reference. For storing data you can use:

  1. Core data
  2. Sqlite
  3. Plist

Upvotes: 1

Related Questions