IB

2012-10-11 2 views
0

버튼을 사용하여 UITableViewCell을 추가하는 방법 내 앱에 메모장 스타일을 추가하는 방법을 시도하고 있습니다. 내가 원했던 것은 오른쪽 상단의 "추가"버튼을 클릭 한 다음 메모를 작성할 수있는 새로운 메모를 만든 다음 완료를 클릭하면 메모를 메모에 추가하는 기존 메모장과 똑같이 작동합니다. UITableView의 셀입니다.IB

난 이미 jQuery과 모든 것이 그냥

이 작업을 실행하는 방법을 알 필요가 설정 한 - (IBAction를) noteAdd : 당신이 것을 클릭하면 (ID) 보낸 사람 { }

그리고 버튼은 내가 위에서 설명한 것을 수행합니다.

어떻게 이렇게 가겠어요? 나는 조금 길다.

이것은 내가 어떻게 TableView를 장면에 추가하는지입니다. 나는 '

: 나는 그것을 코드에 대한 몇 가지 의견을 해요 동안 UITableView

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 

에서

//tableview datasource delegate methods 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
return 1; 
} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
return cameraArray.count; 
} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath{ 
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 


if(cell == nil){ 
    cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:@"Cell"]; 
} 

NSEnumerator *enumerator = [cameraArray objectEnumerator]; 
id anObject; 
NSString *cellName = nil; 
while (anObject = [enumerator nextObject]) { 
    cellName = anObject; 
} 
//static NSString *cellName = [cameraArray.objectAtIndex]; 
cell.textLabel.text = [NSString stringWithFormat:cellName]; 
return cell; 

} 

답변

1

그래서 당신은

-(IBAction) noteAdd:(id)sender 
{ 
    NSIndexPath *newCellPath = [NSIndexPath indexPathForRow:cameraArray.count 
                inSection:0]; 

    // I'm assuming cameraArray is declared mutable. 
    [cameraArray addObject:@"New item"]; 

    [self.tableView insertRowsAtIndexPaths:@[newCellPath] 
          withRowAnimation:UITableViewRowAnimationFade]; 
} 

과 같이 할 것 꽤이 코드 :

NSEnumerator *enumerator = [cameraArray objectEnumerator]; 
id anObject; 
NSString *cellName = nil; 
while (anObject = [enumerator nextObject]) { 
    cellName = anObject; 
} 

은 배열에서 마지막 문자열을 얻는 대신에 원형이됩니다. 너는 cameraArray.lastObject으로 쉽게 할 수있다. 하지만 그건 당신이 중 원하는 것을 생각하지 않는다, 나는 당신이

// XCode >= 4.5: 
cellName = cameraArray[indexPath.row]; 

// XCode < 4.5: 
cellName = [cameraArray objectAtIndex:indexPath.row]; 

그리고 다음 줄을 찾고 생각 :

cell.textLabel.text = [NSString stringWithFormat:cellName]; 

최상의 경우, 이것은 쓸데없는 문자열을 만듭니다. 셀 이름에 %이 있으면 거의 확실하게 오류 또는 EXC_BAD_ACCESS이 표시됩니다. 이 오류를 해결하려면

cell.textLabel.text = [NSString stringWithFormat:@"%@", cellName]; 

그러나 실제로는 이유가 없습니다.

cell.textLabel.text = cellName; 

을 또는 당신은 사본을 주장하는 경우 : 그냥 직접 문자열을 할당

cell.textLabel.text = [NSString stringWithString:cellName]; 
// OR 
cell.textLabel.text = [[cellName copy] autorelease]; 
// OR 
+0

최고, 감사합니다! – tyler53

관련 문제