ios tableviewcontroller
这是一个重写 cell 的方法 ,reuseIdentifier 是 在可是界面里 tableview 的 id
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];
之间的区别
下面是stack 里找到的答案
The most important difference is that the forIndexPath:
version asserts (crashes) if you didn't register a class or nib for the identifier. The older (non-forIndexPath:
) version returns nil
in that case.
You register a class for an identifier by sending registerClass:forCellReuseIdentifier:
to the table view. You register a nib for an identifier by sending registerNib:forCellReuseIdentifier:
to the table view.
If you create your table view and your cell prototypes in a storyboard, the storyboard loader takes care of registering the cell prototypes that you defined in the storyboard.
如果没有注册一个class 或者 一个nib , 使用 前者( forindexpath ) 会报错。
附上stackoverflow 链接 http://stackoverflow.com/questions/25826383/when-to-use-dequeuereusablecellwithidentifier-vs-dequeuereusablecellwithidentifi
http://stackoverflow.com/questions/12737860/assertion-failure-in-deqsueuereusablecellwithidentifierforindexpath
TableView 的数据主要是从 字典(NSMutableDictionary)里取出。
字典(NSMutableDictionary)的数据主要可以 从 plist 文件里取出。
这个tableview
@synthesize listData;
@synthesize dic;
@synthesize teamname;
@synthesize alldic;
- (void)viewDidLoad {
[super viewDidLoad];
NSBundle *bundle=[NSBundle mainBundle];
NSString* filepath=[bundle pathForResource:@"plist" ofType:@"plist"];
self.dic=[[NSMutableDictionary alloc] initWithContentsOfFile:filepath];
self.teamname=[[dic allKeys]sortedArrayUsingSelector:@selector(compare:)];
//self.alldic=dic;
alldic=[[NSDictionary alloc] initWithDictionary:dic];
}
然后是 cell 是单独的一行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//NSLog(@"Here");
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];
if(cell==nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"reuseIdentifier"];
}
NSUInteger section=[indexPath section];//获取到 块号
NSUInteger rows=[indexPath row]; //获取到行号
NSString *name=[self.teamname objectAtIndex:section]; //teamname 是 快号组
NSArray *team=[self.dic objectForKey:name]; //获取 当前快号组 的数组
cell.textLabel.text=[team objectAtIndex:rows]; //得到 text
// Configure the cell...
// cell.textLabel.text=@"s";
return cell;
}
将 tableview 的style 改成 grouped 可改成多 section 的模式
假如要添加 searchbar
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSLog(@"come here");
if([searchText length]==0)
{
[self reset];
}
else
{
[dic removeAllObjects];
//NSArray *allvalue=[alldic allValues];
// 双重循环 ,调用到对象里的 item 然后通过判断 item 里是否存在 一个 searchtext
for (NSString * key in alldic) //通过遍历 teamname里面的key
{
NSLog(@"e");
NSMutableArray *array_=[alldic valueForKey:key];
NSMutableArray *newteams=[[NSMutableArray alloc] init];
for(NSString *tempName in array_)
{
if([tempName rangeOfString:searchText options:NSCaseInsensitiveSearch].location!=NSNotFound)
{
[newteams addObject:tempName];
NSLog(@"zhaodao");
}
}
if(newteams.count>0)
{
NSLog(@"ZHE");
[dic setObject:newteams forKey:key];
}
}
}
}