2011-11-02 3 views
-1

개체가있는 NSMutableArray가 있고 UITableView에서 개체를 추가/제거하는 데 다음 코드를 사용합니다. 앱을 닫은 후 다시 실행 한 후에 만 ​​작동하지만 곧바로 실행되지는 않습니다. 왜 이런거야? 여기 내 코드입니다 (maincelltext은 셀의 제목이며 subtitlecelltext 자막입니다) :NSMutableArray 객체가 UITableView에 바로 추가되지 않는 이유는 무엇입니까?

maincelltext = [[NSMutableArray alloc] init]; 
subtitlecelltext = [[NSMutableArray alloc] init]; 

[maincelltext addObject:@"TITLE"]; 
[subtitlecelltext addObject:@"SUBTITLE TEST"]; 

편집이 :

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

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
} 

cell.textLabel.font = [UIFont fontWithName:@"HelveticaBold" size:16.0]; 

cell.textLabel.adjustsFontSizeToFitWidth = YES; 
cell.textLabel.numberOfLines = 1; 

// Configure the cell. 
UIImage *cellImage = [UIImage imageNamed:@"warning.png"]; 
CGRect cropRect = CGRectMake(0.0, 0.0, 12.0, 12.0); 
CGImageRef croppedImage = CGImageCreateWithImageInRect([cellImage CGImage], CGRectMake(0.0f,0.0f,cellImage.size.width,cellImage.size.height)); 
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:cropRect]; 
[myImageView setImage:[UIImage imageWithCGImage:croppedImage]]; 
CGImageRelease(croppedImage); 
[self.view addSubview:myImageView]; 

cell.imageView.image = cellImage; 

NSString *subjectString = [maincelltext objectAtIndex: [indexPath row]]; 

cell.textLabel.text = subjectString; 

NSString *subtitle = [subtitlecelltext objectAtIndex: [indexPath row]]; 

cell.detailTextLabel.text = subtitle; 

if(maincelltext.count == 0){ 

    NSString *notabledata = [NSString stringWithFormat:@"No Dates Set"]; 
    [maincelltext addObject:notabledata]; 

} 

return cell; 
} 

감사 : 여기 내 jQuery과 코드입니다!

+1

좀 더 자세하게 게시 할 수 있습니까? 이 질문은 다음과 같이 사용할 수 없습니다 –

+0

질문을 이해하고 코드를 게시하기에 충분하지 않습니다. 이 방법으로 테이블에 요소를 추가하거나 제거하지 않고 배열에서 요소를 제거 할 수 있습니다. – Mat

+0

지금 편집 :-) – pixelbitlabs

답변

1

UITableView에 행을 삽입해야한다고 알려줄 필요가 있습니다. 먼저 새 개체 (들)에서 지금 배열의 인덱스를 확인, 다음과 같이 행을 삽입 할 jQuery과 말씀 :

NSIndexPath *indexPathOfNewObject=[NSIndexPath indexPathForRow: newObjectIndex section:0]; 
NSArray *indexPathArray=[NSArray arrayWithObject: indexPathOfNewObject]; 

[self.tableView beginUpdates]; 
// Note that UITableViewRowAnimationAutomatic is for iOS5 only. 
[self.tableView insertRowsAtIndexPaths: indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic]; 
[self.tableView endUpdates]; 

를이 방법을 사용하여, 당신은있는 tableView에 멋진 애니메이션 효과를 얻을 것이다 .

또는 [self.tableView reloadData]를 호출하면 방금 모든 데이터가 다시로드됩니다.

행운을 빈다.

+0

어디서이 코드를 넣을까요? 답변 해 주셔서 감사합니다. :-) – pixelbitlabs

+0

배열에 객체를 추가 한 직후. 그렇게하면 색인을 손에 넣을 수 있습니다. – timthetoolman

+0

테이블 뷰에 변경 사항이 있음을 알리는 코드가 추가되었습니다. – timthetoolman

1

[myArray addObject:myObject];을 수행 한 후에 [myTableView reloadData];을 수행하면 문제가되지 않습니다.

+0

불행히도 여전히 아무 일도 일어나지 않습니다. '( – pixelbitlabs

관련 문제