-1

My method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    NSInteger row = indexPath.row;

    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }

    // Setup row background image (a.k.a the set image)
    NSString *setID = [[self.wallpaperCollectionArray objectAtIndex:row] valueForKey:@"id"];
    NSString *setImageName = [@"s-" stringByAppendingString:setID];
    UIImage *setImage = [UIImage imageNamed:setImageName];

    cell.backgroundView = [[UIImageView alloc] initWithImage:setImage];
    cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:setImage];

    NSString *setName = [[self.wallpaperCollectionArray objectAtIndex:row] valueForKey:@"name"];
    cell.textLabel.text = [setName uppercaseString];
    cell.textLabel.font = [UIFont fontWithName:@"OpenSans" size:20];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.backgroundColor = [UIColor redColor];    // Not working?
    cell.textLabel.textAlignment = NSTextAlignmentCenter;   // Not working?

    return cell;
}

Nevertheless, the result is rather weird, where the backgroundColor and textAlignment do not get applied.

meh!

This is a programmatically created UITableView. What should I do?

UPDATE: the background color issue is solved. Still stumped with the centering.

hfz
  • 317
  • 2
  • 15

5 Answers5

0
 if (nil == cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellIdentifier];
}

NSTextAlignment does not work for styles UITableViewCellStyleValue1 and UITableViewCellStyleDefault .

So if you need to align the textLabel in your cell use UITableViewCellStyleValue2 .

Hope it helps!!!

Benny Dalby
  • 182
  • 4
  • This doesn't seem to work (and doing so cuts off the text on the table cells, too: "BOLD...", "DREAM...", and so on). – hfz Oct 09 '13 at 06:23
0

Suggestion : Instead of doing this alignment programatically , I would suggest you to create a custom cell with nib file containing background image and all other controls as per your requirements. This will help you in future if you need to change the UI . In xib file , you can directly set text alignment also .

iCoder
  • 1,298
  • 1
  • 9
  • 25
0

Try this way..

First set the frame for cell

- (void)layoutSubviews
{
    CGRect textLabelFrame = self.textLabel.frame;
    [super layoutSubviews]

    textLabelFrame.size.width=250.0f;
    self.textLabel.frame = textLabelFrame;
}

Then i think it will work for center Alignment.

Let me know if you have any problem.

user1673099
  • 3,293
  • 7
  • 26
  • 57
0

Found the solution for the centering issue by changing UITableViewCellStyleValue1 to UITableViewCellStyleDefault in

    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
hfz
  • 317
  • 2
  • 15
0

I recommend adding your own UILabel as a subview and using only it and never cell.textLabel. That way you have complete control (without any interference) over text alignment and view frame.

Example:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    NSInteger row = indexPath.row;

    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];

        /* 
         * Make the label cover the entire bounds of the cell, no matter how the cell resizes
         * (Old school autoresizing still works because it gets translated into constraints)
         */
        UILabel *label = [[UILabel alloc] initWithFrame:cell.contentView.bounds];
        label.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

        /*
         * No need to set this stuff repeatedly, let's just do it
         * once on creation of the label
         */
        label.font = [UIFont fontWithName:@"OpenSans" size:20];
        label.textColor = [UIColor whiteColor];
        label.backgroundColor = [UIColor redColor];    // Should work now
        label.textAlignment = NSTextAlignmentCenter;   // Should work now

        // Set a tag so we can find this label later
        label.tag = 42;
    }

    // Setup row background image (a.k.a the set image)
    NSString *setID = [[self.wallpaperCollectionArray objectAtIndex:row] valueForKey:@"id"];
    NSString *setImageName = [@"s-" stringByAppendingString:setID];
    UIImage *setImage = [UIImage imageNamed:setImageName];

    cell.backgroundView = [[UIImageView alloc] initWithImage:setImage];
    cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:setImage];

    NSString *setName = [[self.wallpaperCollectionArray objectAtIndex:row] valueForKey:@"name"];

    // Find our custom UILabel and configure it
    UILabel *label = (id)[self.contentView viewWithTag:42];
    label.text = [setName uppercaseString];

    return cell;
}

Even better, use a UITableViewCell subclass instead. It makes your code more organized. In that case you could use @user1673099's suggestion.