iphone - 在 UITableView 中返回更多单元格

标签 iphone ios uitableview

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

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;
}

最佳答案

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),对其调用自动释放不会执行任何操作。

重用标识符是享元模式。如果 dequeueReusableCellWithIdentifier: 返回 nil,这意味着池中没有单元对象供您使用,您应该为它分配一个新单元对象。 dequeueReusableCellWithIdentifier 也已经返回了一个自动释放对象。

试试这个:

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

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

关于iphone - 在 UITableView 中返回更多单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6219747/

相关文章:

ios - 为什么不应该将 UIWebView 放在 UIScrollView 中?

ios - viewDidLoad多线程问题

ios - 无法在连接的自定义 UITableViewCell 类中创建 socket

ios - TableView - heightForRowAtIndexPath 未被调用

ios - 编译构建失败时出现 xcode 错误

iphone - 使用特定日期将文本保存在 plist 文件中

iphone - 获取 iPhone 的物理屏幕尺寸(以英寸为单位)

iphone - 初始化后如何填充UITableView

iphone - 重新加载自己创建的 UITableViewCell

iphone - 默认的 UITableView 分隔符的 UIColor 是什么?