簡體   English   中英

如何以編程方式在UIview Xib文件上加載UItableList

[英]How to load UItableList Programatically on UIview xib files

嗨,我是iOS的新手,在我的應用程序中,我以編程方式在UIView xib文件上加載UITableView,但使用下面的代碼UItableList不在我的UIView xib文件上添加了我在這里做錯了什么,請幫助我

mycode:-

  #import "FaqQuestionsClass.h"

    @implementation FaqQuestionsClass
    {
        BackGroundClass * Bg;
        UITableView * tableList;
    }
    @synthesize mainView;

    -(instancetype)initWithFrame:(CGRect)frame{

        if (self = [super initWithFrame:frame]) {

            [self loadingView];
        }
        return self;
    }

    - (void)awakeFromNib{

        [super awakeFromNib];

        [self loadingView];
    }

    -(void)loadingView{

        Bg = [[BackGroundClass alloc]init];

        tableList = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableList.translatesAutoresizingMaskIntoConstraints = NO;
        tableList.dataSource=self;
        tableList.delegate=self;
        tableList.backgroundColor = [UIColor orangeColor];
        tableList.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        tableList.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableList.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        [tableList registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        [mainView addSubview:tableList];
    }

    //UITableView Delegate Methods:-

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

        return  10;
    }
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

        return 1;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier   forIndexPath:indexPath] ;

        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        cell.textLabel.text = @"hello";

        return cell;
    }

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        return 50;
    }

    @end

您為表格視圖設置了錯誤的框架。 CGRectZero意味着表視圖在mainView上不可見。 更新下面的行

tableList = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];

與:-

tableList = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM