Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing an array to table Views in Iphone/Objective C
    primarykey
    data
    text
    <p>I am trying to pass an array of float data to a table view but didn't manage to get it working. Currently I am just passing an array with text to be displayed, which was based on an example. Ideally I would like to use the result of calculations being put into an array to fill the table view. What would you suggest to accomplish this task?</p> <p><strong>Button</strong></p> <pre><code>-(IBAction) selectDate:(id)sender; { if ( [[[sender titleLabel] text] isEqualToString:@"Set Start Date"]) { startDate = [pickerDate date]; [startDate retain]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yy"]; labelStartDate.text = [dateFormat stringFromDate:startDate]; [dateFormat release]; //buttonDateStart.enabled = NO; } if ( [[[sender titleLabel] text] isEqualToString:@"Set End Date"]) { endDate = [pickerDate date]; [endDate retain]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yy"]; labelEndDate.text = [dateFormat stringFromDate:endDate]; </code></pre> <p><strong>Data being used</strong></p> <pre><code>- (void)viewDidLoad { NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys: @"MacBook" , @"Name",@"White",@"Color",nil]; NSDictionary *row2 = [[NSDictionary alloc] initWithObjectsAndKeys: @"MacBook pro",@"Name",@"Silver",@"Color",nil]; NSDictionary *row3 = [[NSDictionary alloc] initWithObjectsAndKeys: @"iMac",@"Name",@"White",@"Color",nil]; NSArray *array = [[NSArray alloc] initWithObjects: row1,row2,row3, nil]; self.computers = array; [row1 release]; [row2 release]; [row3 release]; </code></pre> <p><strong>Table</strong></p> <pre><code>-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return[self.computers count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellTableIdentifier = @"CellTableIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier]; if(cell == nil){ CGRect cellFrame = CGRectMake(0,0,300,65); cell = [[[UITableViewCell alloc] initWithFrame: cellFrame reuseIdentifier:CellTableIdentifier] autorelease]; //create the 2 rows CGRect nameLabelRect = CGRectMake(0,5,70,15); UILabel *nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect]; nameLabel.textAlignment = UITextAlignmentRight; nameLabel.text = @"Date:"; nameLabel.font = [UIFont boldSystemFontOfSize:12]; [cell.contentView addSubview: nameLabel]; [nameLabel release]; CGRect colorLabelRect = CGRectMake(0,26,70,15); UILabel *colorLabel = [[UILabel alloc] initWithFrame:colorLabelRect]; nameLabel.textAlignment = UITextAlignmentRight; colorLabel.text = @"Amount:"; colorLabel.font = [UIFont boldSystemFontOfSize:12]; [cell.contentView addSubview: colorLabel]; [colorLabel release]; CGRect nameValueRect = CGRectMake(80,5,200,15); UILabel *nameValue = [[UILabel alloc] initWithFrame:nameValueRect]; nameValue.tag = kNameValueTag; [cell.contentView addSubview:nameValue]; [nameValue release]; CGRect colorValueRect = CGRectMake(80,25,200,15); UILabel *colorValue = [[UILabel alloc] initWithFrame:colorValueRect]; colorValue.tag = kColorValueTag; [cell.contentView addSubview:colorValue]; [colorValue release]; } //Data source NSUInteger row = [indexPath row]; NSDictionary *rowData = [self.computers objectAtIndex:row]; UILabel *name = (UILabel *)[cell.contentView viewWithTag:kNameValueTag]; name.text = [rowData objectForKey:@"Name"]; UILabel *color = (UILabel *)[cell.contentView viewWithTag:kColorValueTag]; color.text = [rowData objectForKey:@"Color"]; return cell; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload