2010-06-01 3 views
0

프로그래밍 방식으로 UITableView의 맨 아래에 UITableViewCell을 추가하려고합니다. 카운트가 나는, 나는 그것을 만들려하지 않는 경우는 더 많은 셀을 반환 할 항목 + 1이면수동으로 UITableViewCell을 추가하는 중 문제가 발생했습니다.

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

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

    static NSString *CellIdentifier = @"Cell"; 

    if (indexPath.row == [items count] + 1) { 
     UITableViewCell* cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MoreCell"] autorelease]; 
     cell.textLabel.text = @"More..."; 
     return cell; 
    } 
    else { 
     STUser *mySTUser; 
     mySTUser = [items objectAtIndex:indexPath.row]; //!!INDEX OUT OF BOUNDS HERE!! 

     AsyncImageView* asyncImage = nil; 
     UILabel* loginLabel = nil; 
     UILabel* fullNameLabel = nil; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) { 
      CGRect frame = CGRectMake(0, 0, 44, 44); 
      CGRect loginLabelFrame = CGRectMake(60, 0, 200, 10); 
      CGRect fullNameLabelFrame = CGRectMake(60, 20, 200, 10); 
      cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease]; 
      asyncImage = [[[AsyncImageView alloc]initWithFrame:frame] autorelease]; 
      [cell.contentView addSubview:asyncImage]; 
      loginLabel = [[[UILabel alloc] initWithFrame:loginLabelFrame] autorelease]; 
      [cell.contentView addSubview:loginLabel]; 
      fullNameLabel = [[[UILabel alloc] initWithFrame:fullNameLabelFrame] autorelease]; 
      [cell.contentView addSubview:fullNameLabel];  
      asyncImage.tag = IMAGE_TAG; 
      loginLabel.tag = LOGIN_TAG; 
      fullNameLabel.tag = FULL_NAME_TAG;  
     } 
     else { 
      asyncImage = (AsyncImageView *) [cell.contentView viewWithTag:IMAGE_TAG]; 
      loginLabel = (UILabel *) [cell.contentView viewWithTag:LOGIN_TAG]; 
      fullNameLabel = (UILabel *) [cell.contentView viewWithTag:FULL_NAME_TAG]; 
     } 

     // Configure the cell... 

     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

     CGRect frame = CGRectMake(0, 0, 44, 44); 
     asyncImage = [[[AsyncImageView alloc]initWithFrame:frame] autorelease]; 
     NSURL* url = [NSURL URLWithString:mySTUser.avatar_url_large]; 
     [asyncImage loadImageFromURL:url]; 
     asyncImage.tag = IMAGE_TAG; 
     [cell.contentView addSubview:asyncImage]; 
     CALayer * l = [asyncImage layer]; 
     [l setMasksToBounds:YES]; 
     [l setCornerRadius:5.0]; 

     CGRect loginLabelFrame = CGRectMake(60, 0, 200, 20); 
     loginLabel = [[[UILabel alloc] initWithFrame:loginLabelFrame] autorelease]; 
     loginLabel.text = [NSString stringWithFormat:@"%@",mySTUser.login]; 
     loginLabel.tag = LOGIN_TAG; 
     [cell.contentView addSubview:loginLabel]; 

     CGRect fullNameLabelFrame = CGRectMake(60, 20, 200, 20); 
     fullNameLabel = [[[UILabel alloc] initWithFrame:fullNameLabelFrame] autorelease]; 
     fullNameLabel.text = [NSString stringWithFormat:@"%@ %@",mySTUser.first_name, mySTUser.last_name]; 
     fullNameLabel.tag = FULL_NAME_TAG; 
     fullNameLabel.font = [UIFont fontWithName:@"Helvetica" size:(12.0)]; 
     [cell.contentView addSubview:fullNameLabel];  

     return cell; 
    } 
} 

: 내가 해결하는 방법을 잘 모르겠습니다 경계 오류 중 인덱스를 얻고있다 다른 모든 세포. 코드가 결코 그 지점에 도달해서는 안되기 때문에 왜 인덱스를 벗어 났는지 확신 할 수 없습니다.

답변

0

Objective-C의 배열과 거의 모든 다른 프로그래밍 언어는 0부터 시작합니다. 즉, 첫 번째 객체에는 인덱스 0이 있고 마지막 객체에는 index (length - 1)가 있습니다. 따라서

, 질병

if (indexPath.row == [items count] + 1) { 

if (indexPath.row == [items count]) { 
1

indexPath.row 변경되어야하는 기초 제로이다.

변경 :
if (indexPath.row == [items count] + 1) {
사람 :
if (indexPath.row == [items count]) {

관련 문제