一些tableView的基础Knowledge(2015414)

来源:互联网 发布:金10数据官网 双十一 编辑:程序博客网 时间:2024/05/17 02:34
1.xcode 6.2
删除原有的ViewController,添加一个tableViewController出现下面的情况。
“Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?”
需要在main.storyboard设置,设置tableViewController选中 【Is Initial View Controller】


2.比较有用的代码块
关于KVC编程,将plist中的文件抽象出来,下面的代码是初始化会用到的代码。
-(instancetype)initWithDict:(NSDictionary *)dict
{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}


+(instancetype)<#name#>WithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}


+(NSArray *)<#name#>
{
    NSArray *array=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"<#plist#>" ofType:nil]];
    NSMutableArray *arrM=[NSMutableArray array];
    for (NSDictionary *dict in array) {
        [arrM addObject:[self <#name#>WithDict:dict]];
    }
    return arrM;
}
PS:这里的plist只是最简单类型:Array-Dictionary 若是更复杂的清苦需要嵌套。


3.关于tableView加载数据
如果是UITableViewDataSource代理实现两个方法,下面方法是其中之一,另一个easy,不多说
UITableViewDataSource(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *ID=@"Cell";
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
   ……    
}
上面这段数据可以重复使用cell,大大提高效率,过后可能更简洁的方法实现


4.打开iphone的通讯录,可以发现右侧的的字母ABCD...索引,下面这个方法就是实现这个功能。
#pragma mark tableView的右侧的索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
   return 【某一个数组】 
}


5.常用的TableView的代理方法
#pragma mark 返回UITableViewCellEditingStyle 的类型
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath


#pragma mark 视tableView能够实现拖动的效果
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
     if(editingStyle==UITableViewCellEditingStyleDelete)
     {
         [self.tableList removeObjectAtIndex:indexPath.row];
//        [self.tableView reloadData];//重新加载数据,刷新列表
         //实现动态的效果
         [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
     }
     else if(editingStyle==UITableViewCellEditingStyleInsert)
     {
         [self.tableList insertObject:@"你好" atIndex:indexPath.row+1];
//       [self.tableView reloadData];
         NSIndexPath *path=[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
         [self.tableView insertRowsAtIndexPaths:@[path]  withRowAnimation:UITableViewRowAnimationFade];
     }
}


6.像button那样的控件可以添加监听的方法,但是tableView等很多控件无法这样做,但是有了代理,这样就方便了,从这个意义上讲代理模式相当给力。





























































































0 0
原创粉丝点击