如何UITableViewCell在表格视图中使用s 内的自动布局,让每个单元格的内容和子视图确定行高(本身/自动),同时保持平滑的滚动性能?
我有三个不同的UITableViews,每个都在它自己的视图中,通过标签访问.理想情况下,所有这三个表都共享相同的自定义UITableViewCell类和.xib文件.
我从一个表开始,将.xib的类设置为我的自定义类,将.xib的文件所有者设置为表的父UIViewController,这非常有效.所有与自定义视图相关的代码都在单元格的类中(基于控制器设置的属性的背景图像,基于标签根据控制器设置的单元属性所需的行数自定义单元格高度等) .
结果很好:单元负责所有可视布局并响应单元控件上的用户操作,而视图控制器负责创建单元格并设置其数据.
既然我需要在其他表中重用单元格,那么自定义单元格的.xib具有单个文件所有者的事实是个问题.而不是复制.xib文件,有一种简单的方法允许多个控制器拥有它吗?
我在iPhone开发学习过程中很早.我正试图绕过各种各样的东西.现在我刚刚采用了基本的NavigationController模板,我正在尝试使用几个文本字段创建一个简单的分组表视图.我似乎无法做的是让Interface Builder允许我将UITableViewCell放入UITableView,以便我可以向Cell添加一个文本字段.这甚至是可能的(似乎它应该被赋予UITableViewCell是一个可拖动控件的事实)?
如果没有,这是否意味着所有这些都是我需要自己编写的代码?
在我的应用程序中,我需要删除表中的多行,编辑表并在表旁边的复选框.选中后,表格单元格将被删除.它就像iPhone消息应用程序.我怎么能这样做,请帮助我.
我尝试在自定义uitableviewcell中使用autolayout并尝试根据此SO主题实现动态高度
在UITableView中使用自动布局来获取动态单元格布局和可变行高
但我的单元格是由xib文件创建的.所以方法有些不同这是我在heightForRowAtIndexPath中的代码
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
myDataItem *item= [[data items] objectAtIndex:[indexPath row]];
CustomCell *cell = [self.offscreenCells objectForKey:@"CustomCell"];
if (!cell) {
cell = [CustomCell alloc] init];
[self.offscreenCells setObject:cell forKey:@"CustomCell"];
}
[cell.commentView setText:item.comment];
[cell.displayNameView setText:item.displayNameOfItemOwner];
[cell.timeAgoView setText:item.timeAgo];
[cell.imageView setImage:[UIImage imageNamed:@"sis_profile_placeholder"]];
cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(_prototypeCell.bounds));
[cell setNeedsLayout];
[cell layoutIfNeeded];
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height+1; }
Run Code Online (Sandbox Code Playgroud)
我从[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]获得的高度总是"无""当我尝试调试时,我发现我的customcell的所有子视图都是"nil",但是单元格本身不是"nil".可能是因为我的customCell是由nib创建的,当我[[CustomCell alloc] init]时,单元格没有完全创建.
但我确实尝试将方法更改为cell = [_ tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
结果仍然相同.每个子视图都是零.这使得高度返回为零并且使得我的tableView显示不正确.有什么方法可以解决这个问题吗?
我尝试在AutoLayout上制作这些.实际上,在之前的版本中,我没有使用AutoLayout并手动计算单元格高度.它确实工作得很好.无论如何,我只想尝试AutoLayout.
请帮忙.谢谢
我将UITableViewCell子类化为自定义它,但我认为我错过了一些东西,因为:1)它不起作用2)有一些我很困惑的事情.除了自定义.xib文件的外观之外,我还更改了backgroundView,并且该部分工作正常.我最不理解/最困惑的部分是init方法,所以我在这里发布了.如果事实证明这是正确的,请告诉我,以便我可以发布可能是原因的更多代码.
这是我定制的init方法.我对"风格"的想法感到困惑,我想我只是用一个不同的backgroundView返回一个普通的UITableViewCell.我的意思是,那里没有任何东西引用.xib或做任何事情,只是从自己改变.backgroundView:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier wait: (float) wait fadeOut: (float) fadeOut fadeIn: (float) fadeIn playFor: (float) playFor
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
CueLoadingView* lview = [[CueLoadingView alloc] initWithFrame:CGRectMake(0, 0, 320, 53)];
self.backgroundView = lview;
[self setWait:wait]; // in turn edits the lview through the backgrounView pointer
[self setFadeOut:fadeOut];
[self setFadeIn:fadeIn];
[self setPlayFor:playFor];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
除了.xib和几个setter和getter之外,这是我的代码中唯一真正的部分,它与检索单元格有关.
附加信息:
1)这是我的.xib,它与班级相关联.

2)这是调用/创建UITableView(委托/视图控制器)的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"CueTableCell"; …Run Code Online (Sandbox Code Playgroud) 我正在从plist中将图像添加到我的单元格视图中.此外,我对使用网址保存到plist的图像也有同样的问题.它们的大小不同.我想将它们调整到一定的大小,以便它们看起来都一样.我试过用:
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
Run Code Online (Sandbox Code Playgroud)
那没起效.我尝试过其他一些东西并没有成功.我查了一堆东西,找不到任何有用的东西.这是我的行方法单元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"city"];
cell.detailTextLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"state"];
cell.imageView.image = [UIImage imageNamed:[[self.searchResults objectAtIndex:indexPath.row] valueForKey:@"cityImage"]];
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
} else {
cell.textLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"city"];
cell.detailTextLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"state"];
// …Run Code Online (Sandbox Code Playgroud) 我正在尝试将一个自定义单元格加载到一个UITableView并且它不断抛出错误
UITableView dataSource必须从tableView:cellForRowAtIndexPath返回一个单元格:
我不知道为什么.我已将我的表视图单元格链接到我的代码中的UITableViewCell定义,但它一直给我这个错误.这是我的代码; 任何帮助将不胜感激.
#import "RegisterDeviceViewController.h"
@implementation RegisterDeviceViewController
@synthesize checkString;
@synthesize cellRegistration;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
//Change UITableView Style to Grouped
- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and want to …Run Code Online (Sandbox Code Playgroud) 对于警报对话框,我添加了一个UITableView,然后当我尝试对Table View Cell进行操作时,Interface Builders不接受将Table View Cell放在“ on”表视图上,请检查屏幕快照,我应该提到根是UIView,而不是UIViewController:
uitableview ×9
ios ×8
iphone ×4
objective-c ×4
autolayout ×2
cocoa-touch ×2
custom-cell ×1
datasource ×1
ios4 ×1
ios7 ×1
nsautolayout ×1
reusability ×1
row-height ×1
swift3 ×1
xcode ×1
xib ×1