2010-06-01 79 views
0

我想以編程方式在我的UITableView底部添加UITableViewCell。我得到一個索引出界失誤,這我不知道如何解決:手動添加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; 
    } 
} 

如果計數我想回到的多個小區,如果沒有,那麼我想它來創建項目+ 1所有其他細胞。不知道爲什麼我的索引超出範圍,因爲代碼永遠不會達到這一點。

回答

0

Objective-C中的數組和幾乎所有其他編程語言都是從零開始的。這意味着第一個對象的索引爲0,最後一個對象的索引爲(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]) {