Ios 以编程方式添加UIControl

Ios 以编程方式添加UIControl,ios,objective-c,uicontrol,Ios,Objective C,Uicontrol,我尝试以编程方式将UITableView添加到scrollview中。我已经设置了锚和数据源,委托。你可以在代码中看到。但我不能。我设置了一个断点,一切都运行了。但我看不到我的滚动视图 UITableView *tableView = [[UITableView alloc] init]; tableView.rowHeight = 130; tableView.translatesAutoresizingMaskIntoConstrai

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

     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;
    }

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

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

    static NSString *identity = @"CommentTableViewCell";
    CommentTableViewCell *cell = (CommentTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identity];
    if(cell == nil){
        @try {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CommentTableViewCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
        @catch (NSException *exception) {
            NSLog(@"Err:%@", exception.reason);
        }
        @finally {

        }

    }

在头文件.h中添加下一个代码:

 <UITableViewDelegate,UITableViewDataSource>
在此之后,您将添加到scrollview中

scrollview.addSubview(yourTableView)

在头文件.h中添加下一个代码:

 <UITableViewDelegate,UITableViewDataSource>
在此之后,您将添加到scrollview中

scrollview.addSubview(yourTableView)

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];


    }
-(void)viewDidLoad{
[超级视图下载];
self.leftLabel=0;
self.topLabel=0;
self.statusWeb=0;
[self.scrollView setBackgroundColor:[UIColor redColor]];
[lblFavoritesComment setText:[NSString stringWithFormat:@“%ld”(长)[[u productModel totalFavorite]];
[lblCommentCount setText:[NSString stringWithFormat:@“%ld”,长)[[u productModel totalComments]];
[lblProductName setText:[\u productModel ProductName]];
lblDescription.lineBreakMode=UILineBreakModeWordWrap;
lblDescription.numberOfLines=0;
[lblDescription setText:[\u productModel Description]];
[LBL说明尺寸合适];
如果([\u productModel serviceHour]==0){
NSString*serviceMinStr=[NSString stringWithFormat:@“%ld dk.”,(长)[[U productModel serviceMin]];
[lblTime setText:serviceMinStr];
}
如果([\u productModel serviceHour]=0&&[\u productModel serviceMin]=0){
[lblTime setText:@”“;
}
如果(![\u productModel.Recipe是kindof类:[NSNull类]]){
如果(![\u productModel.Recipe IsequalString:@“NULL”]| | | u productModel.Recipe.length>0){
self->statusWeb=1;
_webView=[[UIWebView alloc]init];
_webView.translatesAutoresizingMaskIntoConstraints=false;
_webView.frame=CGRectMake(0,0200300);
NSMutableString*html=[nsmutablestringwithstring:@'';
[html附录字符串:[[U productModel Recipe]];
[html附录字符串:@”“;
[_WebViewSetBackgroundColor:[UIColor blueColor]];
[_webviewloadhtmlstring:[html描述]baseURL:nil];
[self.viewWebView添加子视图:_webView];
[\u webView.topAnchor constraintEqualToAnchor:self.viewShareBasketExtra.topAnchor常量:10]。活动=是;
[\u webView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor常量:8]。活动=是;
[\u webView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor常量:8]。活动=是;
}
}否则{
}
int multiPriceStatus=(int)[u productModel multiPrice];
如果(多重属性==1){
[自选价格];
}否则{
UILabel*lblPrice=[[UILabel alloc]init];
[lblPrice setFont:[UIFont fontWithName:@“HelveticaNeue”大小:14];
lblPrice.translatesAutoresizingMaskIntoConstraints=假;
[lblPrice setTextAlignment:NSTextAlignmentCenter];
lblPrice.text=[NSString stringWithFormat:@“%.2f TL”,[\u productModel Price]];
[self.priceView addSubview:lblPrice];
[lblPrice.leftAnchor constraintEqualToAnchor:self.priceView.leftAnchor常量:8]。active=YES;
[lblPrice.rightAnchor constraintEqualToAnchor:self.priceView.rightAnchor常量:8]。活动=是;
[lblPrice.centerXAnchor constrainteQualtoancher:self.priceView.centerXAnchor].active=YES;
[lblPrice.centerYAnchor constraint质量锚定:self.priceView.centerYAnchor].active=YES;
[自我评价];
}
NSString*photo=[\u productModel ProductImage];
photo=[photo stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[imgProduct setShowActivityIndicator视图:是];
[imgProduct setIndicatorStyle:UIActivityIndicatorViewStyleGray];
[imgProduct sd_setImageWithURL:[NSURL URLWithString:photo]
占位符图像:[UIImage ImageName:@“占位符”]选项:/*禁用代码*/(0)==0?SDWebImageRefreshCached:0];
[imgProduct setContentMode:UIViewContentModeScaleAspectFit];
QewerFavoriteSpirate*FavoriteSpirate=[[QewerFavoriteSpirate alloc]initWithTarget:self action:@selector(addFavorite:)];
FavoriteSpirate.productId=[\u productModel Id];
FavoriteShipt.numberOfTapsRequired=1;
[imgFavorite setUserInteractionEnabled:是];
[imgFavorite AddGestureRecognitor:FavoriteShip];
QewerFavoriteSpirate*BasketPirate=[[QewerFavoriteSpirate alloc]initWithTarget:self action:@selector(showBasketWindow:)];
basketposition.productId=[\u productModel Id];
BasketSpiration.numberOfTapsRequired=1;
[imgBasket setUserInteractionEnabled:是];
[imgBasket AddGestureRecognitor:Basket手势];
QewerFavoriteSpirate*extraspirate=[[QewerFavoriteSpirate alloc]initWithTarget:self action:@selector(openExtras:)];
extrastive.productId=[\u productModel Id];
extrastive.numberOfTapsRequired=1;
[imgExtra setUserInteractionEnabled:是];
[imgExtra AddGestureRecognitor:外部手势];
QewerFavoriteSpirate*CommentPirate=[[QewerFavoriteSpirate alloc]initWithTarget:self action:@selector(openComments:)];
commentsignature.productId=[\u productModel Id];
commentsignature.numberOfTapsRequired=1;
[lblCommentCount setUserInteractionEnabled:是];
[lblCommentCount AddGestureRecognitor:注释手势];
}

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