Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Tiiba

macrumors newbie
Original poster
Jul 9, 2010
11
0
I have a table, and I want each cell to contain, besides a bit of text (textLabel) and a picture (imageView), also two custom buttons that would respod to clicks. But they won't show up.

What I did was create a subclass of UIView, to which I added the buttons. I gave the buttons text to display. I called sizeToFit on each button, and then on the container. Then, I added the new view to the cell as accessoryView. I ran the program, and saw that my effort was for naught.

The view I want to see:
PHP:
-(id) init
{
	button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	
	[button1 sizeToFit];[button2 sizeToFit];[self sizeToFit];
	
	item = i;
	send.titleLabel.text = @"1";
	feedback.titleLabel.text = @"2";
	
	
	[self addSubview:button1];
	[self addSubview:button2];
	
	return self;
}

The cell:

PHP:
- (UITableViewCell* )tableView:(UITableView* )tableView cellForRowAtIndexPath:(NSIndexPath* )indexPath
{
	UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"123"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"123"];

    }	
	cell.accessoryView = [[ItemCellAccessory alloc] init];
	[cell.accessoryView retain];
	return cell;
}
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
First, let's start by looking at problems with the code you supplied, Tiiba.
PHP:
-(id) init 
{ 
    button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     
    [button1 sizeToFit];[button2 sizeToFit];[self sizeToFit]; 
     
    item = i;
    send.titleLabel.text = @"1"; 
    feedback.titleLabel.text = @"2"; 
     
     
    [self addSubview:button1]; 
    [self addSubview:button2]; 
     
    return self; 
}
Where and how are send and feedback defined?

Also, what is the purpose of this line?:
Code:
item = i;
 

Tiiba

macrumors newbie
Original poster
Jul 9, 2010
11
0
Ah, sorry. I changed the code from the original to make it more generic-looking (which might not have served any purpose, now that I think of it), and forgot a few things. send and feedback are button1 and button2
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
init methods must call super. UIView subclasses should override initWithFrame: (and call super initWithFrame:).
 

Tiiba

macrumors newbie
Original poster
Jul 9, 2010
11
0
Okay, it turns out you have to add things to the cell's contentView. Fine. But I have another question: when can you find out the table's width? I keep finding out that it's null, no matter what I try. The same thing happens when I try to find out the size of the window.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Well, I included that. It didn't help.

I also tried using a plain ol button, defined as so:

PHP:
UIButton* bt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
bt.titleLabel.text = @"1";

cell.accessoryView = bt;

Nothing chages on the screen.

I also tried adding the button directly to the cell object, with the same effect.
Your UIButton needs its frame set. Otherwise, it is defaulted to CGRectZero, which isn't going to show anything.
 

Tiiba

macrumors newbie
Original poster
Jul 9, 2010
11
0
PHP:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"123"];
	UIButton* bt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	bt.titleLabel.text = @"1";
	bt.titleLabel.frame = CGRectMake(0, 0, 50,50);
	bt.frame = CGRectMake(0, 0, 50,50);
	
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"123"];
    }	
	[cell.accessoryView addSubview:bt];
	return cell;
}

This doesn't work.

And in the contnt view, I can see the button, but not what's in it.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
PHP:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"123"];
	UIButton* bt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	bt.titleLabel.text = @"1";
	bt.titleLabel.frame = CGRectMake(0, 0, 50,50);
	bt.frame = CGRectMake(0, 0, 50,50);
	
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"123"];
    }	
	[cell.accessoryView addSubview:bt];
	return cell;
}

This doesn't work.
Setting the accessoryView and adding a subview to it are two different things, especially if you consider what the value of accessoryView is before calling addSubview: on it.

And in the contnt view, I can see the button, but not what's in it.
Think about the frame for contentView, as well as its relation to textLabel.
 

Tiiba

macrumors newbie
Original poster
Jul 9, 2010
11
0
Setting the accessoryView and adding a subview to it are two different things, especially if you consider what the value of accessoryView is before calling addSubview: on it.

Ah. Thanks.

Think about the frame for contentView, as well as its relation to textLabel.

Don't get that one. I was talking about the button. Anyway, I made it the accessory view now, but it still has no text in it.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
This thread baffles me. The topic of table views is one of the most documented and blogged about and sample coded topics in the known universe.

OP, read the docs, look at the blogs and sample code. The answer is there.

Look at Apple's AccessoryView sample code, if you must do this with an accessory.
 

Tiiba

macrumors newbie
Original poster
Jul 9, 2010
11
0
That's because UIButton's textLabel property is read-only. You should look at the instance methods to determine how to set it.

I didn't try to replace the text label. I set its text value.

And the docs make no mention of textLabel, only titleLabel. Is that what you mean?
 

Tiiba

macrumors newbie
Original poster
Jul 9, 2010
11
0
So, you think you should be able to change the property of an object that is read-only?

Far be it from me. But UILabel.text is read-write. I confirmed this.

Or are you saying that if a read-only property points to an object, THAT obect's properties are fixed?
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Or are you saying that if a read-only property points to an object, THAT obect's properties are fixed?
I thought that's what I was trying to say (sounds logical doesn't it?) but seems Apple is trying to mess with me. So, for a UIButton, those properties of the titleLabel that have corresponding instance methods must use those instance methods and not access the properties directly. See setTitle:forState:, setTitleColor:forState:, etc.
 

Tiiba

macrumors newbie
Original poster
Jul 9, 2010
11
0
I thought that's what I was trying to say (sounds logical doesn't it?) but seems Apple is trying to mess with me.

Well, no, not with my background. In Java, you can use a final method to get an instance of a final class, assign it to a final field, and then modify any of its non-final fields (if they're visible in this scope) all day long. Same in C#, plus readonly properties, unless I'm mistaken.

For the record, I figured out what you were referring to. Thanks.

(Do you have an answer to the other question, regarding objects' sizes?)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.