Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-30-2010, 07:48 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 7
Alec is on a distinguished road
Default How to change UITableViewCell style and insert buttons to UITableView?

Hi, I have a couple of problems.
The first is: how to change UITableViewCell style during UITableView usage. The logic how to use styles in the same table should be:
1. Set UITableViewCellStyleValue2
2. Set UITableView to edit- mode
3. Set UITableViewCellStyleSubtitle

I have try two different identifier to use right cells but it didn't work well or I did it wrong.

The reason why I want to change style during usage is because in table edit- mode I need add images to cells. Images should be kind of buttons where to set my own data properties.
Second question: Is it even possible to set own squares to the same area with cell images?
Third: is it possible insert more than one "real" button to cell (I already have UITableViewCellAccessoryDetailDisclosureButton in use)?

Last edited by Alec; 07-30-2010 at 08:09 AM. Reason: Fix to second question
Alec is offline   Reply With Quote
Old 07-30-2010, 08:33 AM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Ukraine
Posts: 18
kenshin is on a distinguished road
Default

if i correct realized you:

Code:
UIButton *yourButton = [[UIButton buttonWithType: UIButtonTypeRoundedRect];
button.frame = CGRectMake( ?, ? ,?, ?);

NSMutablseArray *cells = [[NSMutableArray alloc] init];
for (int i = 0; i < numberOfNeededCells; i++){
[cells addObject: [[[UITableViewCell alloc] initWithFrame: CGRectMake(0, 0 , cellWidth, cellHeight)] autorelease];
} // how  much cells in table you need

UITableViewCell *cell = [cells objectAtIndex:0]
[cell addSubview:youButton];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
P.S. can be errors in cide

Last edited by kenshin; 07-30-2010 at 08:55 AM.
kenshin is offline   Reply With Quote
Old 07-30-2010, 08:49 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

I would recommend against what kenshin posted.

All of this should be handled in cellForRowAtIndexPath.

To set the UITableView to edit mode, use:
Code:
[self.tableView setEditing:YES];
You can then use properties like tableView.editing to determine what cells you need to provide.

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

         if (tableView.editing){
		UITableViewCell * editingCell = [tableView dequeueReusableCellWithIdentifier:@"EditingCell"];
		if (editingCell == nil){
			editingCell == [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"EditingCell"] autorelease];
		}
		
		UIButton * aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
		[aButton setFrame:CGRectMake(x, y, width, height)];
		[aButton setTitle:@"Button" forState:UIControlStateNormal];
		[aButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
		[editingCell.contentView addSubview:editingCell];
		
		return editingCell;
	}
	else
	{
		UITableViewCell * normalCell = [tableView dequeueReusableCellWithIdentifier:@"NormalCell"];
		if (normalCell == nil){
			normalCell == [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"NormalCell"] autorelease];
		}
		
		[cell.textLabel setText:@"Some text"];
		[cell.detailTextLabel setText:@"Some more text"];
		
		return normalCell;
	}
}
harrytheshark is offline   Reply With Quote
Old 07-30-2010, 08:55 AM   #4 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Ukraine
Posts: 18
kenshin is on a distinguished road
Default

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Yes, all must be here, simply i think that topic starter know, where must be code ^^, because i write so small code

Last edited by kenshin; 07-30-2010 at 08:59 AM.
kenshin is offline   Reply With Quote
Old 07-30-2010, 09:00 AM   #5 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

cellForRowAtIndexPath is called for every row, putting your code in there creates a leaking NSMutableArray containing more cells than is needed (just 1).

You're also creating cells without a style or reuseIdentifier.

Finally, UIButton doesn't have an initWithType method.
harrytheshark is offline   Reply With Quote
Old 07-30-2010, 02:20 PM   #6 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Ukraine
Posts: 18
kenshin is on a distinguished road
Default

if you are not understand it was only little example, not complete code, that you can paste in your project. So if topic starter have basic programming skills he can correct use this code. Or no?

p.s. offtop
kenshin is offline   Reply With Quote
Old 07-30-2010, 06:42 PM   #7 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

If you can't code it yourself, give ideas which the poster can write their own code from.

It's never good to post code that just won't work and isn't logical.
harrytheshark is offline   Reply With Quote
Old 08-02-2010, 06:24 AM   #8 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 7
Alec is on a distinguished road
Default That solve my problems

Now I use only UITableViewCellStyleValue2 style with new UIImageView in normal mode and UIButton in table editing- mode.
There was no problem to use examples so next time I will send my questions to the advanced forum. Thank you for everyone guys.
Alec is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 338
16 members and 322 guests
appservice, bignoggins, dermotos, Domele, EXOPTENDAELAX, guusleijsten, Hamad, heshiming, linkmx, mariano_donati, Objective Zero, Paul Slocum, Rudy, Sloshmonster, teebee74
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,653
Threads: 94,115
Posts: 402,888
Top Poster: BrianSlick (7,990)
Welcome to our newest member, ohmniac
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:47 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0