#import "TableViewController.h"
#import "SearchBar.h"
#import "TableView.h"
@interface TableViewController ()
@end
@implementation TableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
SearchBar *searchBar = [[SearchBar alloc] initWithFrame:CGRectMake(0, 20, 320, 30)];
searchBar.delegate = self;
TableView *tableView = [[TableView alloc] initWithFrame:CGRectMake(0, 50, 320, 430)];
tableView.delegate = self;
//tableView.backgroundColor = [UIColor blackColor];
tableView.backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 430)];
self.tableView = tableView;
[self.tableView addSubview:searchBar];
NSArray *name = @[@"Conan 1",@"Angela 2",@"Aaron 3",@"Alex 4",@"Allen 5",@"Ben 6",@"Bonnie 7",@"Camille 8",@"Carol 9",@"Caroline 10",@"Justin 11",@"Henry 12",@"Yuki 13",@"Cherry 14",@"Dide 15",@"Doris 16",@"Edio 17",@"Eissan 18",@"Flora 19",@"George 20"];
_name = name;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
return [_name count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"NameReuseable";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSLog(@"%d",indexPath.row);
cell.textLabel.text = [_name objectAtIndex:indexPath.row];
//cell.textLabel.text=[NSString stringWithFormat:@"%d",indexPath.row];
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// 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;
}
*/
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
这是代码 当我刚运行程序的时候没问题 可是只要一上下滚动就会报错 说cell.textLabel.text = [_name objectAtIndex:indexPath.row];这里有exc_bad_access 求解啊
//cell.textLabel.text=[NSString stringWithFormat:@"%d",indexPath.row];
另外如果我换成cell.textLabel.text=[NSString stringWithFormat:@"%d",indexPath.row];这样就没有问题 上下滚动都没问题 请问是什么原因呢?