以编程方式添加UIControl

时间:2019-04-15 16:44:39

标签: ios objective-c uicontrol

我尝试以编程方式将UITableView添加到scrollview中。我已经设置了锚点和数据源,委托。您可以在代码中看到。但是我不能。我设置了一个断点,一切正常。但是我在滚动视图中看不到。

onResponse()

此代码有效:

     UITableView *tableView = [[UITableView alloc] init];
            tableView.rowHeight = 130;
            tableView.translatesAutoresizingMaskIntoConstraints = false;
            tableView.delegate = self;
            tableView.dataSource = self;
            tableView.backgroundColor = [UIColor clearColor];

            [self.scrollView addSubview:tableView];

            [tableView.leftAnchor constraintEqualToAnchor:self.scrollView.leftAnchor constant:8].active = YES;
            [tableView.rightAnchor constraintEqualToAnchor:self.scrollView.rightAnchor constant:8].active = YES;
            [tableView.topAnchor constraintEqualToAnchor:self.viewShareBasketExtra.topAnchor constant:8].active = YES;
[tableView reloadData];

但是该代码不起作用:

 UILabel *lblPrice = [[UILabel alloc] init];
        [lblPrice setFont:[UIFont fontWithName:@"HelveticaNeue" size:14]];
        lblPrice.translatesAutoresizingMaskIntoConstraints = false;
        [lblPrice setTextAlignment:NSTextAlignmentCenter];
        lblPrice.text = [NSString stringWithFormat:@"%.2f TL",[_productModel Price]];
        [self.priceView addSubview:lblPrice];

        [lblPrice.leftAnchor constraintEqualToAnchor:self.priceView.leftAnchor constant:8].active = YES;
        [lblPrice.rightAnchor constraintEqualToAnchor:self.priceView.rightAnchor constant:8].active = YES;
        [lblPrice.centerXAnchor constraintEqualToAnchor:self.priceView.centerXAnchor].active = YES;
        [lblPrice.centerYAnchor constraintEqualToAnchor:self.priceView.centerYAnchor].active = YES;
        [self getComments];

我的委托方法:

UITableView *tableView = [[UITableView alloc]init];
            tableView.rowHeight = 130;
            tableView.translatesAutoresizingMaskIntoConstraints = false;
            tableView.delegate = self;
            tableView.dataSource = self;
            tableView.backgroundColor = [UIColor clearColor];

            [self.scrollView addSubview:tableView];

            [tableView.leftAnchor constraintEqualToAnchor:self.scrollView.leftAnchor constant:8].active = YES;
            [tableView.rightAnchor constraintEqualToAnchor:self.scrollView.rightAnchor constant:8].active = YES;
            [tableView.topAnchor constraintEqualToAnchor:self.webView.topAnchor constant:8].active = YES;

我正在使用自定义表格单元格:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(commentsArray!=nil){
        return [commentsArray count];
    }else
    {
        return 0;
    }

}

2 个答案:

答案 0 :(得分:0)

在您的头文件中。h添加下一个代码:

 <UITableViewDelegate,UITableViewDataSource>

例如在UIVIewController类中

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>

这就是为您准备的所有头文件,现在进入.m并实现de nextlines: 这是为了高度:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{return 40.0f}

然后为此添加您的滚动视图

scrollview.addSubview(yourTableView)

答案 1 :(得分:0)

didLoad,现在是UILabel * lblPrice = [[UILabel alloc] init];工作正常,但_webView = [[UIWebView alloc] init];不起作用。

 - (void)viewDidLoad {
        [super viewDidLoad];
        self.leftLabel = 0;
        self.topLabel = 0;
        self.statusWeb = 0;
        [self.scrollView setBackgroundColor:[UIColor redColor]];
        [lblFavoritesComment setText:[NSString stringWithFormat:@"%ld",(long)[_productModel totalFavorite]]];
        [lblCommentCount setText:[NSString stringWithFormat:@"%ld",(long)[_productModel totalComments]]];
        [lblProductName setText:[_productModel ProductName]];
        lblDescription.lineBreakMode = UILineBreakModeWordWrap;
        lblDescription.numberOfLines = 0;
        [lblDescription setText:[_productModel Description]];
        [lblDescription sizeToFit];
        if([_productModel serviceHour] == 0){
            NSString *serviceMinStr = [NSString stringWithFormat:@"%ld dk.",(long)[_productModel serviceMin]];
            [lblTime setText:serviceMinStr];
        }
        if([_productModel serviceHour ] == 0 && [_productModel serviceMin] == 0){
            [lblTime setText:@""];
        }
        if(![_productModel.Recipe isKindOfClass:[NSNull class]]){
            if(![_productModel.Recipe isEqualToString:@"NULL"] || _productModel.Recipe.length>0){
                self->statusWeb = 1;
                _webView = [[UIWebView alloc] init];
                _webView.translatesAutoresizingMaskIntoConstraints = false;
                _webView.frame = CGRectMake(0,0,200, 300);
                NSMutableString *html = [NSMutableString stringWithString: @"<html><head><title></title></head><body style=\"background:transparent;\">"];

                [html appendString:[_productModel Recipe]];
                [html appendString:@"</body></html>"];
                [_webView setBackgroundColor:[UIColor blueColor]];
                [_webView loadHTMLString:[html description] baseURL:nil];

                [self.viewWebView addSubview:_webView];

                [_webView.topAnchor constraintEqualToAnchor:self.viewShareBasketExtra.topAnchor constant:10].active = YES;
                [_webView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor constant:8].active = YES;
                [_webView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor constant:8].active = YES;


            }
        }else{
        }
        int multiPriceStatus = (int)[_productModel multiPrice];
        if(multiPriceStatus == 1){
            [self getMultiPrice];
        }else{
            UILabel *lblPrice = [[UILabel alloc] init];
            [lblPrice setFont:[UIFont fontWithName:@"HelveticaNeue" size:14]];
            lblPrice.translatesAutoresizingMaskIntoConstraints = false;
            [lblPrice setTextAlignment:NSTextAlignmentCenter];
            lblPrice.text = [NSString stringWithFormat:@"%.2f TL",[_productModel Price]];
            [self.priceView addSubview:lblPrice];

            [lblPrice.leftAnchor constraintEqualToAnchor:self.priceView.leftAnchor constant:8].active = YES;
            [lblPrice.rightAnchor constraintEqualToAnchor:self.priceView.rightAnchor constant:8].active = YES;
            [lblPrice.centerXAnchor constraintEqualToAnchor:self.priceView.centerXAnchor].active = YES;
            [lblPrice.centerYAnchor constraintEqualToAnchor:self.priceView.centerYAnchor].active = YES;
            [self getComments];
        }

        NSString *photo = [_productModel ProductImage];
        photo = [photo stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding];
        [imgProduct setShowActivityIndicatorView:YES];
        [imgProduct setIndicatorStyle:UIActivityIndicatorViewStyleGray];

        [imgProduct sd_setImageWithURL:[NSURL URLWithString:photo]
                      placeholderImage:[UIImage imageNamed:@"placeholder"] options:/* DISABLES CODE */ (0) == 0 ? SDWebImageRefreshCached : 0];
        [imgProduct setContentMode:UIViewContentModeScaleAspectFit];

        QewerFavoriteGesture *favoriteGesture =  [[QewerFavoriteGesture alloc] initWithTarget:self action:@selector(addFavorite:)];
        favoriteGesture.productId = [_productModel Id];
        favoriteGesture.numberOfTapsRequired = 1;
        [imgFavorite setUserInteractionEnabled:YES];
        [imgFavorite addGestureRecognizer:favoriteGesture];

        QewerFavoriteGesture *basketGesture =  [[QewerFavoriteGesture alloc] initWithTarget:self action:@selector(showBasketWindow:)];
        basketGesture.productId = [_productModel Id];
        basketGesture.numberOfTapsRequired = 1;
        [imgBasket setUserInteractionEnabled:YES];
        [imgBasket addGestureRecognizer:basketGesture];

        QewerFavoriteGesture *extraGesture =  [[QewerFavoriteGesture alloc] initWithTarget:self action:@selector(openExtras:)];
        extraGesture.productId = [_productModel Id];
        extraGesture.numberOfTapsRequired = 1;
        [imgExtra setUserInteractionEnabled:YES];
        [imgExtra addGestureRecognizer:extraGesture];

        QewerFavoriteGesture *commentGesture =  [[QewerFavoriteGesture alloc] initWithTarget:self action:@selector(openComments:)];
        commentGesture.productId = [_productModel Id];
        commentGesture.numberOfTapsRequired = 1;
        [lblCommentCount setUserInteractionEnabled:YES];
        [lblCommentCount addGestureRecognizer:commentGesture];


    }
相关问题