我对“scrollToRowAtIndexPath”有疑问。我得到这个错误。 (我已经上传了课程)
Posted
技术标签:
【中文标题】我对“scrollToRowAtIndexPath”有疑问。我得到这个错误。 (我已经上传了课程)【英文标题】:I have a problem with 'scrollToRowAtIndexPath'. I get this error. (I have uploaded the classes) 【发布时间】:2011-01-26 16:06:39 【问题描述】:所以首先你可以在这里下载课程: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
【问题讨论】:
messages = [[[NSMutableArray alloc] initWithObjects:@"Hi",nil] 保留]; - 这会导致泄漏;删除保留 【参考方案1】:- (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] )
【讨论】:
row (-1) - 表示索引肯定有问题。通过在使用它的任何地方设置断点来检查它 断点没有解决我的问题..你可以通过下载类试试吗?以上是关于我对“scrollToRowAtIndexPath”有疑问。我得到这个错误。 (我已经上传了课程)的主要内容,如果未能解决你的问题,请参考以下文章
UITableView scrollToRowAtIndexPath
在 iOS 8 上,scrollToRowAtIndexPath 无法正确滚动到底部
scrollToRowAtIndexPath 在标题部分内滚动
scrollToRowAtIndexPath 上非常奇怪的 UITableViewController.tableview 行为