PDA

View Full Version : Table cell accessory view




Tiiba
Jul 14, 2010, 06:22 PM
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:

-(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:


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



PhoneyDeveloper
Jul 14, 2010, 08:35 PM
Take a look at Apple's TableViewSuite sample code for ways to implement custom cells.

dejo
Jul 14, 2010, 08:51 PM
First, let's start by looking at problems with the code you supplied, Tiiba.

-(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?:
item = i;

Tiiba
Jul 14, 2010, 09:12 PM
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
Jul 14, 2010, 09:43 PM
init methods must call super. UIView subclasses should override initWithFrame: (and call super initWithFrame:).

Tiiba
Jul 15, 2010, 10:00 AM
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
Jul 15, 2010, 11:28 AM
Well, I included that. It didn't help.

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

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.

dejo
Jul 15, 2010, 12:04 PM
Okay, it turns out you have to add things to the cell's contentView. Fine.
No, you can set the accessoryView, like you were doing before you edited your post. You just need to make sure the view you're setting has its frame set.

Tiiba
Jul 15, 2010, 12:19 PM
- (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
Jul 15, 2010, 01:13 PM
- (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
Jul 15, 2010, 01:51 PM
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
Jul 15, 2010, 01:53 PM
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.

dejo
Jul 15, 2010, 01:56 PM
Anyway, I made it the accessory view now, but it still has no text in it.
That's because UIButton's textLabel property is read-only. You should look at the instance methods to determine how to set it.

Tiiba
Jul 15, 2010, 02:10 PM
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?

dejo
Jul 15, 2010, 02:28 PM
I didn't try to replace the text label. I set its text value.
So, you think you should be able to change the property of an object that is read-only?

And the docs make no mention of textLabel, only titleLabel. Is that what you mean?
Oops, sorry. Yeah, that's the one. My bad.

Tiiba
Jul 15, 2010, 02:43 PM
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
Jul 15, 2010, 03:15 PM
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
Jul 15, 2010, 03:27 PM
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?)

tutiplain
Mar 16, 2011, 08:53 PM
ok