【问题标题】:how to change width UITableViewCell and use in UITableView如何更改宽度 UITableViewCell 并在 UITableView 中使用
【发布时间】:2014-05-20 21:06:30
【问题描述】:

我有一个类ViewController,我用代码而不是nib文件在其中创建TableView,并且我想要具有nib文件的UITableViewCell的自定义单元格。 我使用宽度为 320px 的代码创建 TableView,我想使用 UITableViewCell 创建自定义单元格,我的单元格宽度为 300px,并放入我的 TableView 中。

注意:我希望这些单元格放在我的表格中心,并且这些单元格距离两侧有 10px 的距离!!!

这是我的代码:

ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    table = [self makeTableView];
    [self.view addSubview:table];
    [self.view setBackgroundColor: RGB(193,60,46)]; //will give a UIColor objct
    name = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
    // Do any additional setup after loading the view from its nib.
}
- (BOOL)prefersStatusBarHidden
{
    return YES;
}
-(UITableView *)makeTableView
{
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    CGRect tableFrame = CGRectMake(x, y, width, height);

    UITableView *tableView = [[UITableView alloc]initWithFrame:tableFrame style:UITableViewStyleGrouped];

    tableView.rowHeight = 60;
    tableView.sectionFooterHeight = 22;
    tableView.sectionHeaderHeight = 22;
    tableView.scrollEnabled = YES;
    tableView.showsVerticalScrollIndicator = YES;
    tableView.userInteractionEnabled = YES;
    tableView.bounces = YES;
    tableView.backgroundColor = [UIColor clearColor];
    tableView.delegate = self;
    tableView.dataSource = self;

    return tableView;
}

#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CustomCell *cell = (CustomCell *)[self.table dequeueReusableCellWithIdentifier:@"myCell"];
    if (cell == nil)
    {
        NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];

        for (id currentObject in topLevelObject) {
            if ([currentObject isKindOfClass:[CustomCell class]]) {
                cell = (CustomCell *)currentObject;
                break;
            }
        }
    }
    // Configure the cell...
    cell.titleLable.text = [name objectAtIndex:indexPath.row];

    return cell;
}

这是我的代码:TableCode

【问题讨论】:

  • 一种方法是保持自定义单元格宽度不变并开始对齐单元格内容以从 10px 开始。
  • 在您的自定义 nib 文件中,保持单元格的宽度与 UIViewController 相同。根据需要放置单元格的所有内容,例如开始将 UILabel 放置在 x = 10px、y = 0px 和所需的宽度和高度。
  • 我好困惑!!!!请告诉我如何在 UITableViewCell 中添加 UIView

标签: ios objective-c uitableview width


【解决方案1】:

afaik,单元格始终是表格的宽度,您无法更改它,您可以通过使单元格的内容视图透明来伪造它,然后放置另一个视图,其中包含您的所有内容,其宽度小于表

【讨论】:

  • 这是真的,单元格的宽度被硬编码以匹配其父 UITableView。默认情况下,它设置为 320 像素。
  • 我的朋友如何创建单元格透明的假内容视图?
  • 在 CustomCell 中覆盖方法“- (void) layoutSubviews”,并通过添加 .backgroundColor = [UIColor clearColor] 使右侧视图透明。
  • mamal10,只需在你的内容视图中添加一个小于内容视图的 UIView,然后将内容视图的背景颜色设置为 clearColor
【解决方案2】:

我有一个宽度为 685 的 UITableView。

我还创建了宽度为 685 的自定义 UITableViewCell。

我添加了两个标签。

我这样对齐标签,左右间距是一样的。

【讨论】:

  • 从对象库中拖放视图并将背景设置为清除颜色
  • 我的朋友我做到了,但我有一个问题如何自定义 UIView 拖动到 CustomCell.m 文件中的单元格???
  • 将它拖到 nib 文件中,然后创建属性。在实现文件里面,你可以设置背景颜色。甚至在属性检查器中。
  • 我的朋友我的代码有问题请下载我的代码并告诉我我的错误在哪里我很困惑!!! :'(
  • 您确定不知道如何在 CustomCell 中添加视图?打开 CustomCell.xib 文件。从对象库中拖放视图。创建属性@property (weak, nonatomic) IBOutlet UIView *customView;在 CustomCell.m 中,添加 customView.backgroundColor = [UIColor clearColor];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
相关资源
最近更新 更多