2011-01-26 50 views
0

所以首先在這裏你可以下載類:http://dl.dropbox.com/u/3951219/classes.zip我有'scrollToRowAtIndexPath'的問題。我得到這個錯誤。 (我已經上傳的類)

我得到這個錯誤:*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:]: row (-1) beyond bounds (0) for section (0).'

這裏的兩個類(它會更好,如果你可以下載一個帶有拉鍊2類和XIB) ChatViewController.h:

#import <UIKit/UIKit.h> 


@interface ChatViewController : UIViewController { 
    IBOutlet UITableView *tbl; 
    IBOutlet UITextField *field; 
    IBOutlet UIToolbar *toolbar; 
    NSMutableArray *messages; 
} 
- (IBAction)add; 

@property (nonatomic, retain) UITableView *tbl; 
@property (nonatomic, retain) UITextField *field; 
@property (nonatomic, retain) UIToolbar *toolbar; 
@property (nonatomic, retain) NSMutableArray *messages; 


@end 

ChatViewController.m:

#import "ChatViewController.h" 


@implementation ChatViewController 
@synthesize tbl, field, toolbar, messages; 

- (void)viewDidLoad { 
    [super viewDidLoad];  

    messages = [[[NSMutableArray alloc] initWithObjects:@"Hi",nil] retain]; 
    [tbl reloadData]; 

} 

- (void)viewWillAppear:(BOOL)animated { 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) 
               name:UIKeyboardWillShowNotification object:self.view.window]; 
} 

- (void)viewWillDisappear:(BOOL)animated { 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
} 

- (void)add { 
    if(![field.text isEqualToString:@""]) 
    { 
     [messages addObject:field.text]; 
     [tbl reloadData]; 
     NSUInteger index = [tbl numberOfRowsInSection:0] - 1; 
     [tbl scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO]; 

     field.text = @""; 
    } 
} 

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    [textField resignFirstResponder]; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 
    toolbar.frame = CGRectMake(0, 372, 320, 44); 
    tbl.frame = CGRectMake(0, 0, 320, 372); 
    [UIView commitAnimations]; 

    return YES; 
} 

- (void)keyboardWillShow:(NSNotification *)notif { 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 
    toolbar.frame = CGRectMake(0, 156, 320, 44); 
    tbl.frame = CGRectMake(0, 0, 320, 156); 
    [UIView commitAnimations]; 

    if([messages count] > 0) 
    { 
     [tbl reloadData]; 
     NSUInteger index = [tbl numberOfRowsInSection:0] - 1; 
     [tbl scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO]; 
    } 
} 

#pragma mark - 
#pragma mark Table view methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [messages count]; 
} 

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

    static NSString *CellIdentifier = @"Cell"; 

    UIImageView *balloonView; 
    UILabel *label; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     tableView.separatorStyle = UITableViewCellSeparatorStyleNone;  

     balloonView = [[UIImageView alloc] initWithFrame:CGRectZero]; 
     balloonView.tag = 1; 

     label = [[UILabel alloc] initWithFrame:CGRectZero]; 
     label.backgroundColor = [UIColor clearColor]; 
     label.tag = 2; 
     label.numberOfLines = 0; 
     label.lineBreakMode = UILineBreakModeWordWrap; 
     label.font = [UIFont systemFontOfSize:14.0]; 

     UIView *message = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)]; 
     message.tag = 0; 
     [message addSubview:balloonView]; 
     [message addSubview:label]; 
     [cell.contentView addSubview:message]; 

     [balloonView release]; 
     [label release]; 
     [message release]; 
    } 
    else 
    { 
     balloonView = (UIImageView *)[[cell.contentView viewWithTag:0] viewWithTag:1]; 
     label = (UILabel *)[[cell.contentView viewWithTag:0] viewWithTag:2]; 
    } 

    NSString *text = [messages objectAtIndex:indexPath.row]; 
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(240.0f, 480.0f) lineBreakMode:UILineBreakModeWordWrap]; 

    UIImage *balloon; 

    if(indexPath.row % 2 == 0) 
    { 
     balloonView.frame = CGRectMake(320.0f - (size.width + 28.0f), 2.0f, size.width + 28.0f, size.height + 15.0f); 
     balloon = [[UIImage imageNamed:@"green.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15]; 
     label.frame = CGRectMake(307.0f - (size.width + 5.0f), 8.0f, size.width + 5.0f, size.height); 
    } 
    else 
    { 
     balloonView.frame = CGRectMake(0.0, 2.0, size.width + 28, size.height + 15); 
     balloon = [[UIImage imageNamed:@"grey.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15]; 
     label.frame = CGRectMake(16, 8, size.width + 5, size.height); 
    } 

    balloonView.image = balloon; 
    label.text = text; 

    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *body = [messages objectAtIndex:indexPath.row]; 
    CGSize size = [body sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(240.0, 480.0) lineBreakMode:UILineBreakModeWordWrap]; 
    return size.height + 15; 
} 

#pragma mark - 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    self.tbl = nil; 
    self.field = nil; 
    self.toolbar = nil; 
} 

/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { 
// Return NO if you do not want the item to be re-orderable. 
return YES; 
} 
*/ 


- (void)dealloc { 
    [tbl release]; 
    [field release]; 
    [toolbar release]; 
    [messages release]; 
    [super dealloc]; 
} 


@end 
+0

消息= [[[NSMutableArray的頁頭] initWithObjects:@ 「嗨」,零]保留]; - 這會導致泄漏;刪除保留 – Max 2011-01-26 16:14:06

回答

0
- (void)add { 
    if(![field.text isEqualToString:@""]) 
    { 
     [messages addObject:field.text]; 
     [tbl reloadData]; 
     NSUInteger index = [tbl numberOfRowsInSection:0] - 1; 
     [tbl scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO]; 
ui  

     field.text = @""; 
    } 
} 

錯誤可能是在這裏,更精確的

,如果: - 在這裏([field.text isEqualToString @ 「」]!)。

if field.text = nil,那麼它會通過條件,這不正確。

你最好嘗試,如果([field.text長度])

+0

不,它不能解決問題:( – cocos2dbeginner 2011-01-26 17:47:38

相關問題