- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 UItableview
,我正在填充数据,使用 heightForRowAtIndexPath
和 cellForRowAtIndexPath
.显然苹果让我在我的代码中做两次事情。
首先,我必须在 heightForRowAtIndexPath
中计算我的 View 的大小(为此我必须制作它们)。然后我必须再次制作它们,将它们添加到实际 View 中。
我有一个非常复杂的 View ,所以当你必须写两次时,它看起来很丑。
难道没有更好的方法来做到这一点吗?
更新
这就是我的代码的外观。它不完全一样,但非常接近。为什么苹果让我写两次?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"heightForRowAtIndexPath");
//Initiating strings
NSString *headlineString;
NSString *subHeadlineString;
NSString *bylineString;
if (global.magazine.issues.count==0) {
return 45;
}else if(indexPath.section == global.magazine.issues.count+1) {
//Finding the right issue and article for this row
Issue *issue = [global.magazine.issues objectAtIndex:global.magazine.issues.count-1];
//Creating the headline
headlineString = [NSString stringWithFormat:@"<span class='bold_style'>FOREWORD</span>"];
//Creating the subHeadline
subHeadlineString = [NSString stringWithFormat:@"%@", [issue.magazine_foreword substringToIndex:100]];
//Creating byline
bylineString = [[NSString stringWithFormat:@"<span class='ital_style'>By %@</span>", issue.magazine_byline] capitalizedString];
}else{
//Finding the right issue and article for this row
Issue *issue = [global.magazine.issues objectAtIndex:indexPath.section-1];
Article *article = [issue.articles objectAtIndex:indexPath.row];
//Creating the headline
headlineString = [NSString stringWithFormat:@"<span class='bold_style'>%@</span>", [article.title uppercaseString]];
//Creating the subHeadline
subHeadlineString = [NSString stringWithFormat:@"%@", [article.main_text substringToIndex:100]];
//Creating byline
bylineString = [NSString stringWithFormat:@"<span class='ital_style'>By %@</span>", article.byline];
}
//Creating the labels
NMCustomLabel *headline = [global.label headLineLabelWithString:headlineString fromTop:30 withWidth:global.screenWidth-60];
NMCustomLabel *subHeadline = [global.label subHeadlineLabelWithString:subHeadlineString fromTop:30+headline.height+10 withWidth:global.screenWidth-60];
NMCustomLabel *byline = [global.label articleBylineLabelWithString:bylineString fromTop:30+headline.height+10+subHeadline.height+10 withWidth:global.screenWidth-60];
//Setting the height of the row
return 30+headline.height+10+subHeadline.height+10+byline.height+30;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"cellForRowAtIndexPath");
//Preparing the cell
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
//Removing former text views
for (UIView *subview in [cell subviews]) {
if (subview.tag == 21 || subview.tag == 22 || subview.tag == 23) [subview removeFromSuperview];
}
//Removing and setting tableview border
[[cell viewWithTag:30] removeFromSuperview];
UIView *rightBorder = [[UIView alloc] initWithFrame:CGRectMake(cell.width-1, 0, 1, cell.height)];
rightBorder.backgroundColor = global.lightGrey;
rightBorder.tag = 30;
[cell addSubview:rightBorder];
//Setting the seletion background color on the cells
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = global.extraLightGrey;
cell.selectedBackgroundView = bgColorView;
if (global.magazine.issues.count==0) {
return cell;
}else if (indexPath.section-1 == global.magazine.issues.count) {
//Finding the right issue and article for this row
Issue *issue = [global.magazine.issues objectAtIndex:global.magazine.issues.count-1];
//Creating the headline
NSString *headlineString = [NSString stringWithFormat:@"<span class='bold_style'>FOREWORD</span>"];
NMCustomLabel *headline = [global.label headLineLabelWithString:headlineString fromTop:30 withWidth:global.screenWidth-60];
headline.tag = 21;
[cell addSubview:headline];
//Creating the subHeadline
NSString *subHeadlineString = [[NSString stringWithFormat:@"%@", issue.magazine_foreword] substringToIndex:100];
NMCustomLabel *subHeadline = [global.label subHeadlineLabelWithString:subHeadlineString fromTop:30+headline.height+10 withWidth:global.screenWidth-60];
subHeadline.tag = 22;
[cell addSubview:subHeadline];
//Creating byline
NSString *bylineString = [[NSString stringWithFormat:@"<span class='ital_style'>By %@</span>", issue.magazine_byline] capitalizedString];
NMCustomLabel *byline = [global.label articleBylineLabelWithString:bylineString fromTop:30+headline.height+10+subHeadline.height+10 withWidth:global.screenWidth-60];
byline.tag = 23;
[cell addSubview:byline];
}else{
//Finding the right issue and article for this row
Issue *issue = [global.magazine.issues objectAtIndex:indexPath.section-1];
Article *article = [issue.articles objectAtIndex:indexPath.row];
//Creating the headline
NSString *headlineString = [NSString stringWithFormat:@"<span class='bold_style'>%@</span>", [article.title uppercaseString]];
NMCustomLabel *headline = [global.label headLineLabelWithString:headlineString fromTop:30 withWidth:global.screenWidth-60];
headline.tag = 21;
[cell addSubview:headline];
//Creating the subHeadline
NSString *subHeadlineString = [NSString stringWithFormat:@"%@", [article.main_text substringToIndex:100]];
NMCustomLabel *subHeadline = [global.label subHeadlineLabelWithString:subHeadlineString fromTop:30+headline.height+10 withWidth:global.screenWidth-60];
subHeadline.tag = 22;
[cell addSubview:subHeadline];
//Creating byline
NSString *bylineString = [[NSString stringWithFormat:@"<span class='ital_style'>By %@</span>", article.byline] capitalizedString];
NMCustomLabel *byline = [global.label articleBylineLabelWithString:bylineString fromTop:30+headline.height+10+subHeadline.height+10 withWidth:global.screenWidth-60];
byline.tag = 23;
[cell addSubview:byline];
}
return cell;
}
最佳答案
最简单的解决方案是遵循 DRY 原则,或者添加高度作为您用作数据源的对象的属性,或者向 View Controller 添加方法,例如:
-(CGFloat)calculateHeightForHeadline:(NSString*)headline andSubHeadline:(NSString*)subHeadline andByLine:(NSString*)byLine
[tableView heightForRowAtIndexPath:indexPath]
从你的 cellForRowAtIndexPath 方法
关于iphone - 丑陋的 heightForRowAtIndexPath 和 cellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14889298/
kable(head(mtcars) %>% kable_styling(bootstrap_options = c("striped", "hover")) 使用 kable 表(见上文)的普通
我试图很好地显示 NSTextView 中突出显示的段落。现在,我通过创建一个带有背景颜色的 NSAttributedString 来做到这一点。这是一些简化的代码: NSDictionary *at
我的站点位于 http://www.idataresearch.net 我正在为导航菜单使用 Dynamic Drive 的 ddmoothmenu 脚本。我真的很喜欢这个脚本,很高兴看到以前的开发人
我在 Ubuntu EC2 实例上运行 nginx、gunicorn、django。整个网站运行良好。除了管理员。管理员无法正常显示。我运行“python manage.py Collectstati
在一个本身嵌套在一个或多个父数组/结构/union 中的数组/结构/union 中读取多个数字/字符串的最佳方法是什么? 没有临时变量的第一个例子: printf("%d %d\n", a[9][3]
我正在编写一个与 mingw 一起使用的包装层,它为应用程序提供了一个虚拟的 UTF-8 环境。处理文件名的函数是从 UTF-8 转换并调用相应的“_w”函数等的包装器。我遇到的最大问题是 Windo
在我的回答中Clojure For Comprehension example我有一个处理自己输出的函数: (defn stream [seed] (defn helper [slow]
我使用的是 bootstrap 4.0 alpha,没有其他样式。布局非常丑陋,日历只显示一列而不是表格。知道为什么以及如何做吗? 我从网站上注意到以下内容,但我不明白我应该怎么做: note: Th
我是一名优秀的程序员,十分优秀!