2011-01-26 3 views
0

여기 그래서 먼저는 클래스를 다운로드 할 수 있습니다 (I는 클래스를 업로드) : 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 
,536,913 63,210
+0

메시지 = [[[있는 NSMutableArray의 ALLOC] initWithObjects : "안녕", 전무] 유지 @] - 이로 인해 누수가 발생합니다. 보유 – Max

답변

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 @ ""]!).

field.text = nil 인 경우 조건을 통과하게되며 이는 사실이 아닙니다.

당신이 더 시도를하려는 경우 ([field.text 길이])

+0

아니요 문제가 해결되지 않는 경우 : ( – cocos2dbeginner

+0

행 (-1) - 인덱스에 확실히 문제가 있음을 의미합니다. 사용되는 모든 지점에서 중단 점을 설정하여 확인하십시오. – Max

+0

중단 점이 내 문제를 해결하지 못했습니다. 수업을 다운로드하여 사용해 볼 수 있습니까? – cocos2dbeginner

관련 문제