pythondocx模块设计表格单元格高度
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pythondocx模块设计表格单元格高度相关的知识,希望对你有一定的参考价值。
参考技术A pythondocx模块设计表格单元格高度?答案是python xlwt 设置单元格样式-合并单元格 xlwt模块详解--合并单元格 import xlwtworkbook = xlwt.Workbook()worksheet =所以以上是pythondocx模块设计表格单元格高度方法。
点击单元格时,表格视图单元格会更改高度
【中文标题】点击单元格时,表格视图单元格会更改高度【英文标题】:Table View Cells change Height when cell tapped 【发布时间】:2014-11-15 22:13:39 【问题描述】:我有一个带有 3 个自定义单元格的 TableView。
当其中一个单元格被点击并进入 WebView 时,表格视图单元格由于某种原因会改变高度。然后,当我从 WebView 返回到 TableView 时,我可以看到 表格视图单元格改变高度。
我认为这与单元格的高度错误有关,但我不确定。这是我尝试过的:
1.
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
id model = self.Model[indexPath.row];
if ([model isKindOfClass:[D self]])
return 490; // As of 11/13/14
else // 2 other custom cells
return tableView.rowHeight; // return the default height
2.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
id model = self.Model[indexPath.row];
if ([model isKindOfClass:[FR self]])
ListTableViewCell *cellOne = [tableView dequeueReusableCellWithIdentifier:@"1Cell"];
CGFloat heightOne = [cellOne.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return heightOne + 2;
else if ([model isKindOfClass:[D self]])
ListTableViewCellTwo *cellTwo = [tableView dequeueReusableCellWithIdentifier:@"2Cell"];
CGFloat heightTwo = [cellTwo.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return heightTwo + 400;
else if ([model isKindOfClass:[YR self]])
ListTableViewCellThree *cellThree = [tableView dequeueReusableCellWithIdentifier:@"3Cell"];
CGFloat heightThree = [cellThree.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return heightThree + 2;
else
return 175;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
id model = self.Model[indexPath.row];
if ([model isKindOfClass:[FR self]])
FR *fD = (FR *)model;
ListTableViewCell *1Cell = [tableView dequeueReusableCellWithIdentifier:@"1Cell"];
NSString *title = [NSString stringWithFormat:@"%@", fD.title];
NSString *dateString = [self timeSincePublished:fD.pubDate];
NSString *description = [self removeHTMLTags:fD.description];
NSString *link = [NSString stringWithFormat:@"%@", fD.link];
1Cell.labelHeadline.text = title;
1Cell.labelDescription.text = description;
1Cell.labelPublished.text = dateString;
return 1Cell;
else if ([model isKindOfClass:[YR self]])
YR *fD = (YR *)model;
ListTableViewCell *3Cell = [tableView dequeueReusableCellWithIdentifier:@"3Cell"];
NSString *title = [NSString stringWithFormat:@"%@", fD.title];
NSString *dateString = [self timeSincePublished:fD.pubDate];
NSString *description = [self removeHTMLTags:fD.description];
NSString *link = [NSString stringWithFormat:@"%@", fD.link];
3Cell.labelHeadline.text = title;
3Cell.labelDescription.text = description;
3Cell.labelPublished.text = dateString;
return 3Cell;
else
D *dD = (D *)model;
ListTableViewCellTwo *2Cell = [tableView dequeueReusableCellWithIdentifier:@"2Cell"];
NSString *dateString = [self timeSincePublished:dD.created_time];
NSString *cap = [NSString stringWithFormat:@"%@", dD.cap.text];
NSString *us = [NSString stringWithFormat:@"%@", dD.us.use];
2Cell.labelHeadline.text = us;
2Cell.labelDescription.text = cap;
2Cell.labelPublished.text = dateString;
return 2Cell;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
// Remove grey background from Highlighted State
[tableView deselectRowAtIndexPath:indexPath animated:YES];
Matt Tang 更新:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
id model = self.Model[indexPath.row];
if ([model isKindOfClass:[FR self]])
ListTableViewCell *1Cell = [tableView dequeueReusableCellWithIdentifier:@"1Cell"];
if (!1Cell)
1Cell = [[ListTableViewCell alloc] init];
FR *fD = (FR *)model;
NSString *title = [NSString stringWithFormat:@"%@", fD.title];
NSString *dateString = [self timeSincePublished:fD.pubDate];
NSString *description = [self removeHTMLTags:fD.description];
1Cell.labelHeadline.text = title;
1Cell.labelDescription.text = [description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
1Cell.labelPublished.text = dateString;
CGFloat heightOne = [1Cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return heightOne + 2;
else if ([model isKindOfClass:[YR self]])
ListTableViewCellThree *3Cell = [tableView dequeueReusableCellWithIdentifier:@"3Cell"];
if (!3Cell)
3Cell = [[ListTableViewCellThree alloc] init];
YR *fD = (YR *)model;
NSString *title = [NSString stringWithFormat:@"%@", fD.title];
NSString *dateString = [self timeSincePublished:fD.pubDate];
NSString *description = [self removeHTMLTags:fD.description];
3Cell.labelHeadline.text = title;
3Cell.labelDescription.text = description;
3Cell.labelPublished.text = dateString;
CGFloat heightThree = [3Cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return heightThree + 2;
else if ([model isKindOfClass:[D self]])
ListTableViewCellTwo *2Cell = [tableView dequeueReusableCellWithIdentifier:@"2Cell"];
if (!2Cell)
2Cell = [[ListTableViewCellTwo alloc] init];
D *dD = (D *)model;
NSString *dateStringI = [self timeSincePublished:dD.created_time];
NSString *TI = [NSString stringWithFormat:@"%@", dD.caption.text];
NSString *us = [NSString stringWithFormat:@"%@", dD.us.use];
2Cell.labelHeadline.text = us;
2Cell.labelDescription.text = cap;
2Cell.labelPublished.text = dateStringI;
CGFloat heightTwo = [2Cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return heightTwo + 450;
else
return 490;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
id model = self.Model[indexPath.row];
if ([model isKindOfClass:[FR self]])
FR *fD = (FR *)model;
ListTableViewCell *1Cell = [tableView dequeueReusableCellWithIdentifier:@"1Cell"];
if (!1Cell)
1Cell = [[ListTableViewCell alloc] init];
FR *fD = (FR *)model;
NSString *title = [NSString stringWithFormat:@"%@", fD.title];
NSString *dateString = [self timeSincePublished:fD.pubDate];
NSString *description = [self removeHTMLTags:fD.description];
1Cell.labelHeadline.text = title;
1Cell.labelDescription.text = description;
1Cell.labelPublished.text = dateString;
return 1Cell;
// more code
FR *fD = (FR *)model;
ListTableViewCell *1Cell = [tableView dequeueReusableCellWithIdentifier:@"1Cell"];
NSString *title = [NSString stringWithFormat:@"%@", fD.title];
NSString *dateString = [self timeSincePublished:fD.pubDate];
NSString *description = [self removeHTMLTags:fD.description];
NSString *link = [NSString stringWithFormat:@"%@", fD.link];
1Cell.labelHeadline.text = title;
1Cell.labelDescription.text = description;
1Cell.labelPublished.text = dateString;
return 1Cell;
CustomCell2.h
(CustomCell2.m
现在什么都没有):
@property (strong, nonatomic) IBOutlet UILabel *labelHeadline;
@property (strong, nonatomic) IBOutlet UILabel *labelPublished;
@property (strong, nonatomic) IBOutlet UILabel *labelDescription;
@property (strong, nonatomic) IBOutlet UIImageView *imageViewPic;
也尝试设置断点。
【问题讨论】:
当你从 webview 返回时,你是否重新加载 table view 单元格?尝试将[[self tableView] reloadData];
放在您的viewWillAppear 中。
@JAL 是的,我试过了。奇怪的部分是它发生在下一个ViewController
以及从下一个ViewController
回来的路上。还有其他想法吗?谢谢!
你能显示在选择单元格时运行的代码吗?因为它只在用户点击一个单元格时发生,也许它在那里......
cellForRowAtIndexPath
或 didSelectRowAtIndexPath
或其他?请告诉我,我很乐意发帖
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath [tableView deselectRowAtIndexPath:indexPath animated:YES];
didSelectRowAtIndexPath
【参考方案1】:
您将不得不改变实现 tableView:heightForRowAtIndexPath: 的方式:
您调用 dequeueReusableCellWithIdentifier: 来获取您的自定义单元格对象以查找其高度。该方法仅跟踪屏幕上可见的单元格。当您离开该表格视图时,它会自行清空。
您需要做的是检查调用 dequeueReusableCellWithIdentifier 后是否返回 nil: *edit 你应该实际使用 dequeueReusableCellWithIdentifier:forIndexPath:
如果为 nil,则必须创建一个临时单元格,对其进行配置,然后返回其高度。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
id model = self.Model[indexPath.row];
if ([model isKindOfClass:[FR self]])
ListTableViewCell *cellOne = [tableView dequeueReusableCellWithIdentifier:@"1Cell" forIndexPath:indexPath];
if (!cellOne)
cellOne = [[ListTableViewCell alloc] init];
FR *fD = (FR *)model;
NSString *title = [NSString stringWithFormat:@"%@", fD.title];
NSString *dateString = [self timeSincePublished:fD.pubDate];
NSString *description = [self removeHTMLTags:fD.description];
NSString *link = [NSString stringWithFormat:@"%@", fD.link];
cellOne.labelHeadline.text = title;
cellOne.labelDescription.text = description;
cellOne.labelPublished.text = dateString;
// Make sure the cell's frame is updated
[cellOne setNeedsLayout];
[cellOne layoutIfNeeded];
CGFloat heightOne = [cellOne.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return heightOne + 2;
else if ([model isKindOfClass:[D self]])
// more code
如果你想遵循 DRY 原则,请创建一个类似的方法
- (ListTableViewCell *)configureCellForModel:(id)model;
并拥有所有基于模型类的自定义单元配置代码。让它返回单元格并在 tableView:cellForRowAtIndexPath: 和 tableView:heightForRowAtIndexPath: 中调用它:
更新示例
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
id model = self.Model[indexPath.row];
if ([model isKindOfClass:[FR self]])
FR *fD = (FR *)model;
ListTableViewCell *1Cell = [tableView dequeueReusableCellWithIdentifier:@"1Cell"];
if (!1Cell)
1Cell = [[ListTableViewCell alloc] init];
FR *fD = (FR *)model;
NSString *title = [NSString stringWithFormat:@"%@", fD.title];
NSString *dateString = [self timeSincePublished:fD.pubDate];
NSString *description = [self removeHTMLTags:fD.description];
1Cell.labelHeadline.text = title;
1Cell.labelDescription.text = description;
1Cell.labelPublished.text = dateString;
return 1Cell;
// more code
【讨论】:
感谢您的回复!self.combinedModel
实际上应该是 self.model
我的错,我改变了它。希望有帮助!
我在回答中添加了一些内容。
是的,如果我按照您的建议进行操作,所有单元格都保持在 100 处,没有跳跃。关于如何获得它的任何想法,因为我有 3 个使用 AutoLayout 的自定义单元格,所以我无法在其中硬编码该值?
非常好,马上尝试实施,会通知您!
(顺便说一句,谢谢,我觉得这是朝着正确的方向发展)以上是关于pythondocx模块设计表格单元格高度的主要内容,如果未能解决你的问题,请参考以下文章