在UITableView中返回更多单元格

时间:2011-06-02 20:03:41

标签: iphone ios uitableview

我正在使用MGTwitterEngine几乎一切都想通了,但由于某种原因,我无法弄清楚如何在我的时间轴中返回更多推文!它通过我正在使用的方法接收大约20条推文,如果我添加一个整数,例如:return count = 100;,我可以告诉我现在有100个单元格加载时和滚动指示器但是当我滚动到那个18- 19单元格我收到此错误:

  

SIGABRT    - [NSMutableArray objectAtIndex:]:索引20超出边界[0 .. 19]'    * 首次调用堆栈:

我知道这意味着什么:没有更多信息可以在第20单元收到。我很困惑,进出MGTwitterEngine寻找默认的推文计数,但我找不到它。我正在使用转储来创建我的NSString,并且转储似乎只能为每次登录提供大约20条推文。请帮忙,我的书中的任何建议都很好!谢谢!

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    int count = [tweets count];
    // Return just enough cells to fill the screen during loading ....
    if (count == 0)
        count = MyCustomRowCount;
        else {
            //Here is where I think I need to add a else return integer but dont know how
        }
    return count;
    return [tweets count];
    return [authors count];
    return [avatarsURL count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) { 
        [cell autorelease];
    }

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"]autorelease];
    /*
     //Here it adds a nice shadow to the table view but will crash on rotation and send a  
     wird dump  !!!???? 
     tableView.layer.shadowColor = [[UIColor blackColor] CGColor];
     tableView.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
     tableView.layer.shadowRadius = 8.0f;
     tableView.layer.shadowOpacity = 1.0f;     
     */
    [cell.textLabel setNumberOfLines:1];
    [cell.textLabel setText:[(Tweet*)[authors objectAtIndex:indexPath.row] author]];  
    [cell.detailTextLabel setText:[(Tweet*)[tweets objectAtIndex:indexPath.row] tweet]];
    [cell.detailTextLabel setNumberOfLines:10];
    [cell.textLabel setTextColor:[UIColor darkGrayColor]];
    [cell.textLabel setShadowColor:[UIColor whiteColor]];
    [cell.textLabel setShadowOffset:CGSizeMake(0.5, 0.5)];
    [cell.detailTextLabel setTextColor:[UIColor blackColor]];
    //[cell.detailTextLabel setText:[(Tweet*)[retweetCount objectAtIndex:indexPath.row] 
    reTweetCount]];
    [cell.textLabel setUserInteractionEnabled:YES];
    [cell.contentView setMultipleTouchEnabled:YES];
    // cell.text = [[NSString alloc] initWithFormat:@"Cell :%i", indexPath.row];    


    // Here we use the new provided setImageWithURL: method to load the web image with 
    SDWebImageManager
    [cell.imageView setImageWithURL:[NSURL URLWithString:[(Tweet*)[avatarsURL   
                                                                   objectAtIndex:indexPath.row]avatarURL]]
                   placeholderImage:[UIImage imageNamed:@"avatar.png"]];

    //add gradient to cell 
    UIImage *gradient = [UIImage imageNamed:@"gradientcell2.png"];
    UIImageView *cellimage = [[UIImageView alloc] initWithImage:gradient];
    cellimage.contentMode = UIViewContentModeScaleToFill;
    cell.backgroundView = cellimage;
    [cellimage release];      

    UIImage *selectedGradient = [UIImage imageNamed:@"selectedcell.png"];
    UIImageView *selectedCell = [[UIImageView alloc] initWithImage:selectedGradient];
    selectedCell.contentMode = UIViewContentModeScaleToFill;
    cell.selectedBackgroundView = selectedCell;    
    [tableView setBackgroundColor:[UIColor clearColor]];     
    return cell;
}

//get cell accessory  
-(UITableViewCellAccessoryType)tableView:(UITableView *)tableView   
accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row < [tweets count]){

    }    
    return UITableViewCellAccessoryDisclosureIndicator;
}

// custom hieght 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
    return 80;
}

//select tweet bring to detail view ..... Also bring in the Users information who made the tweet!
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {

    //Get the selected tweets
    NSString *selectedTweet = [(Tweet*)[tweets objectAtIndex:indexPath.row] tweet]; 
    NSString *selectedUser = [(Tweet*)[tweets objectAtIndex:indexPath.row] author];
    NSString *selectedUserInfo = [(Tweet*)[tweets objectAtIndex:indexPath.row] user];
    NSString *retweetCount = [(Tweet*)[tweets objectAtIndex:indexPath.row] reTweetCount ];
    // NSString *selectedUserFriendsCount = [(Tweet*)[tweets objectAtIndex:indexPath.row] userFriendsCount];------HUH? NO soup for ME!
    NSString *selectedUserFollowersCount = [(Tweet*)[tweets objectAtIndex:indexPath.row] userFollowersCount];   

    //Initialize the detail view controller and display it.
    TwitterDetailViewController*dvController = [[TwitterDetailViewController alloc]
                                                initWithNibName:@"TwitterDetailViewController" bundle:[NSBundle mainBundle]];
    dvController.selectedTweet = selectedTweet;
    dvController.selectedUser = selectedUser;
    dvController.selectedUserInfo = selectedUserInfo;
    // dvController.selectedUserFriendsCount = selectedUserFriendsCount;------Doesnt reconize the call for some odd reason!
    dvController.selectedUserFollowersCount = selectedUserFollowersCount;   
    dvController.retweetCount = retweetCount;
    dvController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:dvController animated:YES];
    //[self.navigationController pushViewController:webController animated:YES];-------would rather use  navagation controller for several obvious reasons
    [dvController release];
    dvController = nil;
}

1 个答案:

答案 0 :(得分:0)

当你给它一些不一致的东西时,UITableView并不喜欢它。如果更改模型,则需要调用[tableView reloadData]或者如果要为更改设置动画,请调用[tableView beingUpdate] [tableView endUpdate],并在中间插入/删除所有插入/移除单元格操作。

另一方面,我不确定你为什么要这样做:

NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (cell == nil) { 
        [cell autorelease];
}

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
reuseIdentifier:@"cell"];
[cell autorelease];

首先,如果(cell == nil),则调用自动释放将无法执行任何操作。

重用标识符是一个flyweight模式。如果dequeueReusableCellWithIdentifier:返回nil,则表示池中没有单元对象供您使用,您应为其分配一个新对象。 dequeueReusableCellWithIdentifier也已经返回一个自动释放对象。

而是试试这个:

NSString *identifier = @"mytweetcell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];
}