简体   繁体   中英

how to change width UITableViewCell and use in UITableView

I have one class ViewController that I to create TableView in it with code not nib file and I want custom cell with UITableViewCell that has nib file. I create TableView with code that has 320px width and I want create custom cell with UITableViewCell that my cells had 300px width and put in my TableView.

notice: I want these cells put center my table and these cells have 10px distance from two side!!!

this is my code:

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;
}

this is my code : TableCode

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

I have a UITableView with a width of 685.

I created Custom UITableViewCell with a width of 685 also.

I have added two labels.

I aligned a label in such a way, the spacing for left and right are the same.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM