• 关于继承UITableViewController若干问题


    //
    //  MSHomeCommentTableViewController.m
    //  xiaoqu-ios
    //
    //  Created by Charlie on 15/7/1.
    //  Copyright (c) 2015年 meimeidou. All rights reserved.
    //
    
    #import "MSHomeCommentTableViewController.h"
    
    @interface MSHomeCommentTableViewController ()<UITabBarControllerDelegate,UITableViewDataSource,UITableViewDelegate>
    {
        BOOL _isMoreDataIng;
        
        
    }
    
    @end
    
    @implementation MSHomeCommentTableViewController
    -(NSMutableArray *)dataArr
    {
        if (!_dataArr)
        {
            _dataArr =[NSMutableArray array];
        }
        return _dataArr;
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        _isMoreDataIng = NO ;
        [self getDataFromServer];
        [self.refreshControl addTarget:self action:@selector(getDataFromServer) forControlEvents:UIControlEventValueChanged];//自带的刷新控件
        [self.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil]; //观察者 观察上拉加载更多数据
    }
    #pragma mark 从服务器获取数据
    - (void)getDataFromServer
    {
        
    }
    - (void)getMoreDataFromServer
    {
        
    }
    #pragma mark -上拉加载更多函数
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        if ([object isKindOfClass:[self.tableView class]]) {
            CGFloat offset=[[change objectForKey:@"new"] CGPointValue].y ;
            
            if (offset > self.tableView.contentSize.height-self.tableView.frame.size.height)
            {
                NSLog(@"%.0f",offset);
                if (!_isMoreDataIng)
                {
                    _isMoreDataIng = YES ; //记录刷新状态
                    [self getMoreDataFromServer];// 记载更多数据
                }
                
            }
            
        }
    }
    /*
    #pragma mark - doneRefresh
    - (void)doneRefresh
    {
        double delayInSeconds = 0.3;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [_refreshControl endRefreshing];
            _isLoadMoreProcessing = NO;
        });
    }
     */
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    #pragma mark - Table view data source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
        // Return the number of rows in the section.
        
        return self.dataArr.count;
    }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (self.clickRowBlock) {
            self.clickRowBlock ((int)indexPath.row); //传递观察者
        }
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 75 ;
    }
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString * cellID = @"ThemeCellId";
        MSThemeTableViewCell * cell ;
        cell =[tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell =[[MSThemeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone ;
        }
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [cell configDataWithModel:self.dataArr[indexPath.row]];
        return cell;
    }
    
    
    
    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
        
    }
    - (void)dealloc
    {
        [self.tableView removeObserver:self forKeyPath:@"contentOffset"]; //删除 观察者
    }
    @end

    可能有些同学也是这样子写的,没问题,但是 就是tableview上的view 粘贴到其他view上好像没有反应,

    _tableView=[[MSHomeCommentTableViewController alloc]init];
         _tableView.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        [self.view addSubview:_tableView.tableView];

    好像没问题,但是还是每出来。

    最后想半天,原来是数据源没有。

    再加上这数据源就OK了

    - (void)configData
    {
        _tableView.dataArr =[_dataArray mutableCopy];
    }

    加上这个函数就OK 了。

  • 相关阅读:
    学习规划
    一位十年的老司机告诉你什么是编程思想
    React开发
    一个简单的ipfs音乐播放器的实现
    React错误总结(三)
    React错误总结解决方案(二)
    mongoid模糊查询
    Rails accepts_nested_attributes_for表单嵌套的利器
    route_path
    "constantize" and "with_indifferent_access" method
  • 原文地址:https://www.cnblogs.com/fgyqbs/p/4615632.html
Copyright © 2020-2023  润新知