2012-03-26 3 views
0

개체 배열의 세부 정보를 채우려는 UITableView가 있습니다. tableview는 모든 라인에 똑같은 항목을 보여줍니다 (올바른 라인 수!). 쉬운 일일 것임을 알고 있습니다. 그러나 어디서 잘못되었는지는 알 수 없습니다 :내 UITableView는 모든 행에 동일한 항목을 표시합니다.

코드 뷰를 초기화하는 코드 스 니펫 테이블 데이터 :

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 

{ 

    if([segue.identifier isEqualToString:@"Show Tank List"]) 

    { 

     NSURL *myUrl = [[NSURL alloc]initWithString:@"http://localhost/~stephen-hill9/index.php"]; 
     NSData *data = [[NSData alloc] initWithContentsOfURL:myUrl]; 
     NSError *error; 
     NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
     int i; 
     NSMutableArray *tanksList; 
     tank *thisTank = [[tank alloc] init]; 
     tanksList = [[NSMutableArray alloc] init]; 
     for (i=0; i<json.count; i++) { 
      NSDictionary *bodyDictionary = [json objectAtIndex:i]; 
      thisTank.tankNumber = [bodyDictionary objectForKey:@"ID"]; 
      thisTank.tankProduct = [bodyDictionary objectForKey:@"Product_Desc"]; 
      thisTank.tankPumpableVolume = [bodyDictionary objectForKey:@"Pumpable"]; 
      [tanksList addObject:thisTank]; 
     } 
     [segue.destinationViewController setTanks:tanksList]; 
    } 
} 

... 그리고 다음보기에서 테이블을로드하는 코드 ...

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1;//keep this section in case we do need to add sections in the future. 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [self.tanks count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Tank List Table Cell"; 
    UITableViewCell *cell = [self.tankTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (!cell) 
    { 
     cell = [[UITableViewCell alloc] initWithFrame:CGRectZero]; 
    } 
    tank *thisTank = [self.tanks objectAtIndex:indexPath.row]; 
    cell.textLabel.text = thisTank.tankNumber; 
    return cell; 
} 

답변

3

이동이 :

tank *thisTank = [[tank alloc] init]; 

for 루프 내부. 동일한 객체를 반복해서 업데이트하고 있습니다. 또한

, 당신은 셀 잘못을 초기화하는 것 - 지정된 initialiser를 사용하고있는 재사용 식별자를 통과, 그렇지 않으면 당신은 새로운 세포를 항상 생성합니다 :

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

을 그리고 당신이 정말로 objective- 따라야합니다 C 명명 규칙. 클래스는 대문자로 시작하고 그 밖의 모든 것은 소문자로 시작합니다. 어쨌든 다른 사람들을 위해 코드를 훨씬 쉽게 읽을 수 있습니다.

+0

Perfect! 고맙습니다! 나는 그것이 쉬운 일이 될 것이라고 생각했다! :-) 나는 루프 밖에서 탱크를 초기화함으로써 값을 변경하고 효과적으로 객체의 사본을 추가한다고 생각했다. 다시 감사 드려요 !!! :-) – HillInHarwich

0

때마다 테이블을 다시로드하십시오 !!!

[self.tableView reloadData];

관련 문제