【发布时间】:2018-05-17 19:11:44
【问题描述】:
我遇到了一个奇怪的问题。我正在为我的表格视图创建一个自定义选定视图。但是,此选择视图不太适合。我发现这是因为我的选择视图是 300 像素,而单元格本身是 320 像素。奇怪的是UITableView 的宽度实际上只有 300px。如何调整表格的单元格宽度?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tableIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier] autorelease];
if (tableView == bandTable)
{
UIImageView *imageBg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"column1_selected.png"]];
[imageBg setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
[cell setSelectedBackgroundView:imageBg];
[imageBg release];
NSArray *array = [bandDictionary objectForKey:@"Bands"];
cell.textLabel.text = [array objectAtIndex:indexPath.row];
NSLog(@"CELL: %f", cell.bounds.size.width);
}
return cell;
}
【问题讨论】:
-
请粘贴您的代码 CellForIndexPath:
-
如果您不确定数据的宽度,而不是尝试使用
UIView来支撑它,您可以使用UIScrollView并确保根据您的设置其contentSize属性需要。您需要向右或向左滚动才能看到剩余(或隐藏)的数据。 -
@Hemang 这不是我关心的数据。这是自定义选择视图不太合适。就像我说的那样,表格和自定义选择视图的宽度都是 300 像素,但单元格宽度是 320 像素。
-
你不能明确地将 imageBg 的框架设置为 imageBg.frame = CGRectMake....
-
您能以编程方式检查表格大小吗?我认为
cellForRowAtIndexPath将是这个检查的好地方。
标签: objective-c ios uitableview