سؤال

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

هل كانت مفيدة؟

المحلول

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.

enter image description here

نصائح أخرى

afaik, cells are always the width of the table and you cant change that, you can fake it by making the content view of the cell transparent, then place another view with all your content in it that is a smaller width than the table

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top