zir*_*isp 211 storyboard uitableview ios ios5 xcode4.2
我正在尝试调整表格视图中其中一个单元格的单元格高度.我正在从相关单元格的"尺寸检查器"中的"行高"设置中调整大小.当我在iPhone上运行应用程序时,单元格的默认大小设置为表格视图中的"行大小".
如果我更改表视图的"行大小",则所有单元格的大小都会更改.我不想这样做,因为我只想要一个单元格的自定义大小.我已经看到很多帖子都有针对该问题的程序化解决方案,但我更愿意通过故事板来实现,如果可能的话.
pix*_*eak 293
在动态单元格rowHeight上,UITableView上的set总是覆盖单个单元格的rowHeight.
但是在静态单元格上,rowHeight在单个单元格上设置可以覆盖UITableView.
不确定这是不是一个bug,Apple可能是故意这样做的?
Beb*_*ber 84
如果您使用UITableViewController,请实现此方法:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
Run Code Online (Sandbox Code Playgroud)
在行的功能中,您可以选择高度.例如,
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return 100;
}
else {
return 60;
}
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,第一行高度为100像素,其他行为60像素.
我希望这个可以帮助你.
mem*_*ons 34
对于动态单元格,rowHeight设置UITableView始终覆盖单个单元格rowHeight.
这个行为是,IMO,一个bug.任何时候你必须在两个地方管理你的UI很容易出错.例如,如果您在故事板中更改单元格大小,则必须记住也要更改它们heightForRowAtIndexPath:.在Apple修复bug之前,当前最好的解决方法是覆盖heightForRowAtIndexPath:,但使用故事板中的实际原型单元来确定高度而不是使用幻数.这是一个例子:
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
/* In this example, there is a different cell for
the top, middle and bottom rows of the tableView.
Each type of cell has a different height.
self.model contains the data for the tableview
*/
static NSString *CellIdentifier;
if (indexPath.row == 0)
CellIdentifier = @"CellTop";
else if (indexPath.row + 1 == [self.model count] )
CellIdentifier = @"CellBottom";
else
CellIdentifier = @"CellMiddle";
UITableViewCell *cell =
[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell.bounds.size.height;
}
Run Code Online (Sandbox Code Playgroud)
这将确保在运行时自动获取原型单元格高度的任何更改,您只需在一个位置管理UI:故事板.
Jos*_*phH 22
我已经构建了各种答案/注释提示的代码,以便这适用于使用原型单元的故事板.
这段代码:
感谢Answerbot,Brennan和lensovet.
- (NSString *)cellIdentifierForIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = nil;
switch (indexPath.section)
{
case 0:
cellIdentifier = @"ArtworkCell";
break;
<... and so on ...>
}
return cellIdentifier;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [self cellIdentifierForIndexPath:indexPath];
static NSMutableDictionary *heightCache;
if (!heightCache)
heightCache = [[NSMutableDictionary alloc] init];
NSNumber *cachedHeight = heightCache[cellIdentifier];
if (cachedHeight)
return cachedHeight.floatValue;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
CGFloat height = cell.bounds.size.height;
heightCache[cellIdentifier] = @(height);
return height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [self cellIdentifierForIndexPath:indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
<... configure cell as usual...>
Run Code Online (Sandbox Code Playgroud)
Cha*_*lva 10
如果您使用swift,请使用这样的方法.不要使用故事板来选择行高.以编程方式设置表行高度,如下所示,
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 || indexPath.row == 1{
let cell = self.tableView.dequeueReusableCellWithIdentifier("section1", forIndexPath: indexPath) as! Section1TableViewCell
self.tableView.rowHeight = 150
cell.label1.text = "hiiiiii"
cell.label2.text = "Huiiilllllll"
return cell
} else {
let cell = self.tableView.dequeueReusableCellWithIdentifier("section2", forIndexPath: indexPath) as! Section2TableViewCell
self.tableView.rowHeight = 60
cell.label3.text = "llll"
return cell
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过以下行从故事板获取UITableviewCell(在UITableviewController - 静态单元格中)的高度.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = [super tableView:tableView heightForRowAtIndexPath:indexPath];
return height;
}
Run Code Online (Sandbox Code Playgroud)
在XML视图中打开storyboard并尝试编辑rowHeight所需元素的属性.
当我尝试为我的原型行设置自定义rowHeight时,它对我有用.它不是通过检查员工作,但通过XML工作.
您可以使用cells自定义原型height,然后调用cellForRowAtIndexPath:并返回它frame.height.:.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tableView
cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
110019 次 |
| 最近记录: |