首先,您可以下载课程:http://dl.dropbox.com/u/3951219/classes.zip
我收到此错误:*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:]: row (-1) beyond bounds (0) for section (0).'
这里有两个类(如果你可以下载带有2个类和xib的zip,会更好) 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 :(得分: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 = @"";
}
}
错误可能在这里,更准确
if(![field.text isEqualToString:@“”]) - 这里。
如果field.text = nil,那么它将通过条件,而这不是真的。
你最好试试if([field.text length])