以编程方式添加 UIControl
Posted
技术标签:
【中文标题】以编程方式添加 UIControl【英文标题】:Add UIControl programmatically 【发布时间】:2019-04-15 16:44:38 【问题描述】:我尝试以编程方式将 UITableView 添加到滚动视图中。我已经设置了锚点和数据源,委托。你可以在代码中看到。但我不能。我放了一个断点,一切都运行了。但我在滚动视图中看不到。
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
【问题讨论】:
是否调用了委托/数据源方法?你的 UITableView 的高度是多少?它应该是什么? 我希望高度是动态的,我正在使用自定义 uitablecell。 '- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView return 1; - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section if(cmetsArray!=nil) return [cmetsArray count]; 其他 返回 0; ' 如果同时添加底部锚点会怎样?表格视图本身是一个滚动视图,将滚动其内容,但没有高度,它将不可见。这可能是您遇到的问题。 【参考方案1】:在你的头文件 .h 中添加下一个代码:
<UITableViewDelegate,UITableViewDataSource>
例如在 UIVIewController 类中
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
这就是你的头文件,现在到 .m 并实现下一行: 这是为了身高:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPathreturn 40.0f
然后你添加到你的滚动视图中
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];
【讨论】:
[self.viewWebView addSubview:_webView];
不应该是[self.view addSubview : _webView ];
以上是关于以编程方式添加 UIControl的主要内容,如果未能解决你的问题,请参考以下文章
以编程方式将 UISearchBar 添加到普通 ViewController 中以编程方式添加的 UITableView