Добавить UIControl программно - PullRequest
1 голос
/ 15 апреля 2019

Я пытаюсь добавить 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 {

        }

    }

Ответы [ 2 ]

0 голосов
/ 15 апреля 2019

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


    }
0 голосов
/ 15 апреля 2019

в вашем заголовочном файле .h добавьте следующий код:

 <UITableViewDelegate,UITableViewDataSource>

Например, в UIVIewController Class

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>

и это все для вас, заголовочный файл теперь получил .mи реализуйте следующие строки: это для высоты:

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

и после этого вы добавляете в свой скроллвид

scrollview.addSubview(yourTableView)
...