07-16-2011, 04:27 PM
#1 (permalink )
Mobile Apps Developer
Join Date: Oct 2010
Posts: 24
How Do I Change a Table View Cell's Subtitle?
Hello. I have a problem that is making me go crazy. Everything I try doesn't work, and I'm not sure what I'm doing wrong. Basically, I'm trying to let the user change the subtitle in all of the table view cells. They just press a button and it would change them. So I tried to do this by setting the NSMutableArray to have different objects. I've tried addObject, replaceObjectAtIndex, and even removeAllObjects. None of them seem to have any effect. So... What should I try?
07-16-2011, 04:53 PM
#2 (permalink )
Emphasizing Fundamentals
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
You aren't really expecting a good answer from this, are you?
07-16-2011, 04:57 PM
#3 (permalink )
Mobile Apps Developer
Join Date: Oct 2010
Posts: 24
Quote:
Originally Posted by
BrianSlick
You aren't really expecting a good answer from this, are you?
I dunno. You want some sample code or something? I thought this was somewhat easy to understand. Could be wrong though.
07-16-2011, 04:58 PM
#4 (permalink )
Emphasizing Fundamentals
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
Need to know what you are doing in order to provide a correction.
07-16-2011, 06:14 PM
#5 (permalink )
Mobile Apps Developer
Join Date: Oct 2010
Posts: 24
Quote:
Originally Posted by
BrianSlick
Need to know what you are doing in order to provide a correction.
When the user taps a button, I want to change the all of the table view cells' subtitles. The table view only has 4 cells.
In the .h I have:
Code:
#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController <UIActionSheetDelegate> {
IBOutlet UINavigationController *mainNavCont;
IBOutlet UITableView *tableView;
NSMutableArray *data;
NSMutableArray *subTitleData;
}
- (IBAction)button:(id)sender;
@end
In the .m, I have:
Code:
#import "MyViewController.h"
@implementation MyViewController
- (IBAction)button:(id)sender {
[subTitleData removeObject:@"5 mins"];
[subTitleData removeObject:@"10 mins"];
[subTitleData removeObject:@"15 mins"];
[subTitleData removeObject:@"20 mins"];
[subTitleData addObject:@"10 mins"];
[subTitleData addObject:@"15 mins"];
[subTitleData addObject:@"20 mins"];
[subTitleData addObject:@"25 mins"];
[tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"] autorelease];
}
cell.textLabel.text = [data objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [subTitleData objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row == 0) {
//haven't gotten to these yet
}
if (indexPath.row == 1) {
}
if (indexPath.row == 2) {
}
if (indexPath.row == 3) {
}
}
- (void)dealloc {
[mainNavCont release];
[tableView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
data = [[NSMutableArray alloc] initWithObjects:@"First", @"Second", @"Third", @"Fourth", nil];
subTitleData = [[NSMutableArray alloc] initWithObjects:@"5 mins", @"10 mins", @"15 mins", @"20 mins", nil];
}
- (void)viewDidUnload {
[mainNavCont release];
mainNavCont = nil;
[tableView release];
tableView = nil;
[super viewDidUnload];
}
@end
Not sure what I'm doing wrong. The subtitles don't change when I tap the button.
07-16-2011, 06:19 PM
#6 (permalink )
Just helping out.
Join Date: Feb 2011
Posts: 2,565
Put an NSLog in the method to make sure it's firing and check the connection on your tableview outlet.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
New app - See screenshots and details at
www.globaclock.com .
If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
07-16-2011, 06:28 PM
#7 (permalink )
Emphasizing Fundamentals
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
Also (probably not related), having a navigation controller in your view controller is probably the wrong thing to be doing.
Since this isn't the best way to set up your data, you should perhaps consider replacing the objects, rather than removing-and-then-adding.
07-16-2011, 06:40 PM
#8 (permalink )
Mobile Apps Developer
Join Date: Oct 2010
Posts: 24
Quote:
Originally Posted by
BrianSlick
Also (probably not related), having a navigation controller in your view controller is probably the wrong thing to be doing.
Since this isn't the best way to set up your data, you should perhaps consider replacing the objects, rather than removing-and-then-adding.
I have tried replacing them using replaceObjectAtIndex, but I cannot use indexPath as the index. I tried just throwing a random number (0) in there, and that didn't work either.
07-16-2011, 06:43 PM
#9 (permalink )
Mobile Apps Developer
Join Date: Oct 2010
Posts: 24
Quote:
Originally Posted by
Domele
Put an NSLog in the method to make sure it's firing and check the connection on your tableview outlet.
I have used NSLog and it is firing. I just check the connection and it's connected too.
07-16-2011, 06:46 PM
#10 (permalink )
Just helping out.
Join Date: Feb 2011
Posts: 2,565
Put this at the end of your IBAction method:
Code:
NSLog(@"%@", subTitleData);
and paste the result here.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
New app - See screenshots and details at
www.globaclock.com .
If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
07-16-2011, 06:57 PM
#11 (permalink )
Mobile Apps Developer
Join Date: Oct 2010
Posts: 24
Quote:
Originally Posted by
Domele
Put this at the end of your IBAction method:
Code:
NSLog(@"%@", subTitleData);
and paste the result here.
I figured out the issue. It was a simple typo. I spelt subTitleData wrong. I copied it correct though haha. Sorry for wasting your time :/
07-16-2011, 09:46 PM
#12 (permalink )
Just helping out.
Join Date: Feb 2011
Posts: 2,565
Wouldn't that cause a compiler error that would say something like undefined pointer?
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
New app - See screenshots and details at
www.globaclock.com .
If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
07-16-2011, 09:48 PM
#13 (permalink )
Emphasizing Fundamentals
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
Quote:
Originally Posted by
JoeRCruso
I have tried replacing them using replaceObjectAtIndex, but I cannot use indexPath as the index. I tried just throwing a random number (0) in there, and that didn't work either.
You only have 1 section, so use the row as the index.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 402
17 members and 385 guests
blasterbr , buggen , Clouds , dre , EvilElf , HemiMG , jeroenkeij , jimmyon122 , jonathandeknudt , LEARN2MAKE , Mah6447 , n00b , nyoe , pungs , Sami Gh , stanny , toon4413
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,668
Threads: 94,121
Posts: 402,900
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jonathandeknudt