2011-06-02 99 views
1

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

SIGABRT -[NSMutableArray objectAtIndex:]: index 20 beyond bounds [0 .. 19]' * Call stack at first throw:

我知道这意味着什么:世界上没有更多的信息,在接收单元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; 
} 
+0

我建议提高代码格式化并添加'iphone'或'ios'标签。 – Anna 2011-06-02 20:14:14

+0

谢谢我做到了。 – FreeAppl3 2011-06-02 20:28:55

+0

你为什么要这样做: if(cell == nil){cell autorelease]; } cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@“cell”]; [cell autorelease]; – 2011-06-03 03:02:34

回答

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),要求其自动释放不会做任何事情。

重用标识符thing是一个flyweight模式。如果dequeueReusableCellWithIdentifier:返回nil,则表示它没有池中的单元对象供您使用,并且应该为其分配一个新对象。 dequeueReusableCellWithIdentifier也已经返回一个autorelease对象。

相反试试这个:

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

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

谢谢扎克!我很感激!我使用你提供的代码不会改变任何东西的功能,但绝对看起来更清洁。我仍然有尝试检索更多推文的问题,然后只有20个。我只能想到它的默认推文计数为20,在MGTwitter Engin的某处,我找到了它。我知道在旧版本中有一个#define DEFAULT_TWEET_COUNT = 20,但现在它消失了,我似乎无法在任何地方找到....所以有可能告诉我的表视图滚动时重新加载数据?如果是的话,那么如何尝试呢? – FreeAppl3 2011-06-03 17:40:11

相关问题