问题
I have created a custom Cell. I have an array of dictionaries. For the dictionary values I need to create UILable
s. Each cell may contained different number of UILabel
s. So in my custom UITableViewCell
class I have done like this.
- (void)generateCell {
BOOL isLandscape = UIInterfaceOrientationIsLandscape(vc.interfaceOrientation);
for (int i = 0; i < [orderMap count]; i++) {
UILabel *lbl = [[UILabel alloc] init];
[lbl setTextColor:[UIColor blueColor]];
[lbl setBackgroundColor:[UIColor yellowColor]];
lbl.tag = [[orderMap objectAtIndex:i] intValue];
[lbl setNumberOfLines:0];
[self.contentView addSubview:lbl];
[lbl setTranslatesAutoresizingMaskIntoConstraints:false];
}
if (isLandscape) {
}
else {
[self setConstraintsForPortrait];
}
}
- (void)setConstraintsForPortrait {
double currentUsedWidth = 0;
double relativeWidthLimit = 1;
int reservedPaddingSpace = 20;
int usableScreenWidthPts = 0;
int xPositionStartingPoint = 15;
int xPosition = xPositionStartingPoint;
int yPosition = INDIVIDUAL_PADDING_PTS;
int fieldCounter = 0;
UIView *previousView;
for (UIView *vw in [self.contentView subviews]) {
NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:(xPosition + INDIVIDUAL_PADDING_PTS)];
NSLayoutConstraint *top;
if (previousView == nil) {
top = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:INDIVIDUAL_PADDING_PTS];
}
else {
top = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:previousView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:INDIVIDUAL_PADDING_PTS];
}
NSLayoutConstraint *trailing = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:INDIVIDUAL_PADDING_PTS];
if (fieldCounter >= [orderMap count]) {
NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:INDIVIDUAL_PADDING_PTS];
[self.contentView addConstraint:bottom];
}
[self.contentView addConstraint:leading];
[self.contentView addConstraint:top];
[self.contentView addConstraint:trailing];
previousView = vw;
}
}
And in my UIViewController
viewDidLoad
method I have done this.
UINib *nib = [UINib nibWithNibName:@"TableViewCell" bundle:[NSBundle mainBundle]];
[self.tbl registerNib:nib forCellReuseIdentifier:metadataListTableIdentifier];
self.tbl.estimatedRowHeight = 200;
self.tbl.rowHeight = UITableViewAutomaticDimension;
[self.tbl reloadData];
Why the cell height is not changing? Please help me. Thanks
回答1:
After setting delegate and datasources of tableview, Try this
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension //return height size whichever you want
}
or
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 200 //return height size whichever you want
}
回答2:
Follow below code:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self getMessageSize:[[_arrCaseActivityList objectAtIndex:indexPath.row] objectForKey:@"message"] Width:275];
}
-(float )getMessageSize :(NSString *)text Width :(float)textWidth
{
NSAttributedString *attributedText =
[[NSAttributedString alloc]
initWithString:text
attributes:@
{
NSFontAttributeName: [UIFont fontWithName:@"Rubik-Regular" size:13.0]
}];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){textWidth, CGFLOAT_MAX}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize finalSize = rect.size;
return finalSize.height+75;
}
回答3:
Provide proper constraints in Interface Builder eg. leading, trailing
etc. and set no of lines
for UILabel as 0. Add the below code in viewDidLoad
method.
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 300 // estimated row height for performance improvement
回答4:
click + Drag your tableview and set viewcontroller view equal width and equal height
#pragma mark tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [DATA count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
PartyListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"PartyListCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
cell.textLabel.text = [DATA objectAtIndex:indexPath.row];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 38.0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *tableviewHeader = [[[NSBundle mainBundle] loadNibNamed:@"PartyListHeaderView" owner:self options:nil] objectAtIndex:0];
return tableviewHeader;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 38.0;
}
click on button load you nib file
if (popUpView== nil) {
popUpView = [[[NSBundle mainBundle] loadNibNamed:@"PopupView" owner:self options:nil] objectAtIndex:0];
[self.view addSubview:popUpView];
[popUpView setDelegate:self];
[popUpView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
}
回答5:
In my case, I was using the function :
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
instead of
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
Make sure you are using the correct function.
来源:https://stackoverflow.com/questions/47935935/uitableview-row-height-not-changing