小编Val*_*sov的帖子

没有contentView框架计算UITableViewCell高度的最佳方法

摘要

鉴于我们并不总是知道单元格的框架或其内容视图将是什么(由于编辑,旋转,附件视图等),tableView:heightForRowAtIndexPath:当单元格包含一个单元格时,计算高度的最佳方法是什么?可变高度文本字段或标签?

我的一个UITableViewController's包含以下演示文稿:UITableViewCell与UITextView.

UITextView的宽度和高度应与UITableViewCell相同.

在此输入图像描述

我创建了UITableViewCell子类,然后使用UITextView初始化它(UITextView是我的UITableViewController的私有字段)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
     static NSString *CellIdentifier = @"TextViewCell";
     UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) {
         cell = [[[BTExpandableTextViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier textView:_notesTextView] autorelease];
     }    
     return cell;
}
Run Code Online (Sandbox Code Playgroud)

我在UITableViewCell子类中实现了以下方法:

- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat height = [textView.text sizeWithFont:textView.font constrainedToSize:CGSizeMake(textView.frame.size.width, MAXFLOAT)].height + textView.font.lineHeight;
    textView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, (height < textView.font.lineHeight * 4) ? textView.font.lineHeight * 4 : height);    
    [self.contentView addSubview:textView];
}
Run Code Online (Sandbox Code Playgroud)

当然我实现了以下UITableViewDataSource方法(看!我使用的是self.view.frame.size.width(但实际上我需要UITableViewCell contentView框架宽度):

- (CGFloat)tableView:(UITableView*)tableView …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c uitableview ios heightforrowatindexpath

9
推荐指数
1
解决办法
4727
查看次数

关于.h和.m文件(如何管理)

嘿有经验的程序员!

我想问你一些干净的代码:)

我想保持我的代码干净,让它变得更好,所以这就是我的问题:

1)我应该把#imports放在哪里?我的原则:(我不认为它们很好)

  • #import框架应始终在.h
  • #import .h文件应始终以.m为单位
  • .h文件应该只有@class,而不是import(不包括UIKit之类的框架等)
  • 代表们应该在.m

    @interface ViewController() <UIAlertViewDelegate> 
    @end
    
    Run Code Online (Sandbox Code Playgroud)

2)我应该在哪里放置我的实例变量?

  • Private和Protected变量必须是.m
  • 公众必须在.h

3)我应该把我的方法放在哪里?

  • 公开在.h
  • 在.m私有(是的,我知道我的"私人方法"并不是真正私密的,只是隐藏)

(顺便说一句,这很明显)

4)#define?怎么样?

  • 全球 - 始终在.h
  • 仅在该类中使用 - 始终在.m中

5)我应该在哪里放置NSNotification全局标识符以及如何组织它们

  • #define NSNotificationDataSourceDidLoadData @"NSNotificationDataSourceDidLoadData"在将发送此通知的类中的.h文件中

但......

  • Apple在.h文件中有很多私有内容
  • 在大多数情况下,我的.h文件只是.. EMPTY :)

一年前,我有另一种情况 - 一切都在.h,但我认为它也很糟糕

该怎么办?你使用什么原则?谢谢!

这是关于编码风格的问题,而不是关于"如何使其可编辑"的问题

objective-c ios

1
推荐指数
1
解决办法
3824
查看次数