Something weird going on here. I have a tableview, and in each row there is a segmentedcontrol. The meaning of this is, that there only has to be one segmented control selected at all time. On a click of a 'control item', there is a segue that is pushed.
If I constantly select items of different segmented controls, there is nothing going on, my code works perfectly. When i select an different item from the same segmented control, i have this error:
nested push animation can result in corrupted navigation bar
Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
Unbalanced calls to begin/end appearance transitions for .
This is my code:
#interface PerformancesController ()
{
GFilm* currentFilm;
GTimePerformance* chosenTimePerf;
}
#end
#implementation PerformancesController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [currentFilm.dayList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"myCustomCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"myCustomCell"];
}
CAGradientLayer *bgLayer = [BackgroundLayer blackGradient];
bgLayer.frame = cell.bounds;
[cell.contentView.layer addSublayer:bgLayer];
cell.contentView.backgroundColor = [UIColor darkGrayColor];
cell.textLabel.textAlignment = UITextAlignmentLeft;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if(indexPath.row == 0)
{
CGRect rctStreep = cell.bounds;
rctStreep.origin.y = rctStreep.origin.y+1;
rctStreep.size.height = 1;
UIImageView* imgv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"streep.png"]];
imgv.backgroundColor = [UIColor blackColor];
imgv.frame = rctStreep;
[cell.contentView addSubview:imgv];
}
CGRect rctStreep = cell.bounds;
rctStreep.origin.y = rctStreep.origin.y + rctStreep.size.height;
rctStreep.size.height = 1;
UIImageView* imgv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"streep.png"]];
imgv.backgroundColor = [UIColor blackColor];
imgv.frame = rctStreep;
[cell.contentView addSubview:imgv];
imgv = nil;
NSArray* dayList = currentFilm.dayList;
GDay* d = (GDay*)[dayList objectAtIndex:indexPath.row];
CGRect rctDayLabel = CGRectMake(15, 7, 80, 30);
UILabel* lblDay = [[UILabel alloc]initWithFrame:rctDayLabel];
lblDay.textColor = [GColor yellowAccentColor];
NSDate* date = d.date;
if([date isToday])
{
lblDay.text = #"Vandaag";
}
else if([date isTomorrow])
{
lblDay.text = #"Morgen";
}
else
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM"];
lblDay.text = [formatter stringFromDate:date];
}
[cell.contentView addSubview:lblDay];
NSMutableArray* itemArray = [[NSMutableArray alloc]init];
int i = 0;
GTimePerformance* prevChosen = currentFilm.displayPerformance;
int gekozen = -1;
for(i; i < [d.timeIdList count]; i++)
{
GTimePerformance* per = [d.timeIdList objectAtIndex:i];
if([per.performanceId isEqualToString:prevChosen.performanceId])
{
gekozen = i;
}
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm"];
[itemArray addObject:[formatter stringFromDate:per.performanceTime]];
}
float itemWidth = (cell.frame.size.width - (2* rctDayLabel.origin.x))/4;
MySegmentedControl *segmentedControl = [[MySegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(rctDayLabel.origin.x, cell.frame.size.height - rctDayLabel.origin.y - 30, itemWidth*[d.timeIdList count] , 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.tintColor = [GColor filterComponentColor];
if(gekozen >= 0)
{
[segmentedControl setSelectedSegmentIndex:gekozen];
}
[segmentedControl addTarget:self
action:#selector(clickedPerformance:)
forControlEvents:UIControlEventValueChanged];
UIFont *font = [UIFont systemFontOfSize:10.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
[segmentedControl setTitleTextAttributes:attributes
forState:UIControlStateNormal];
segmentedControl.tag = 200 + indexPath.row;
[cell.contentView addSubview:segmentedControl];
return cell;
}
-(void)clickedPerformance:(id)sender
{
UISegmentedControl *segment = (UISegmentedControl*)sender;
if(segment.selectedSegmentIndex != -1)
{
int tag = segment.tag;
int i = 0;
for(i; i < [currentFilm.dayList count]; i++)
{
int diffTag = i + 200;
if(diffTag != tag)
{
UISegmentedControl* sControl = (UISegmentedControl*)[self.view viewWithTag:diffTag];
[sControl setSelectedSegmentIndex:UISegmentedControlNoSegment];
}
}
NSLog(#"lalala");
GDay* day = (GDay*)[currentFilm.dayList objectAtIndex:tag-200];
chosenTimePerf = [day.timeIdList objectAtIndex:segment.selectedSegmentIndex];
NSLog(chosenTimePerf.performanceId);
[self performSelector:#selector(goSegue) withObject:nil afterDelay:0.35];
}
}
-(void)goSegue
{
[self performSegueWithIdentifier:#"pushPrices" sender:self];
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSString * segueName = segue.identifier;
if ([segueName isEqualToString: #"pushPrices"]) {
PricesController* pc = (PricesController*)[segue destinationViewController];
[pc setMovie:currentFilm andPerformance:chosenTimePerf];
}
}
#end
Why do you delay the goSegue method? hitting the item multiple times before afterDelay has elapsed will cause trouble.
I would change:
[self performSelector:#selector(goSegue) withObject:nil afterDelay:0.35];
to:
[self goSegue];
Related
I have a method setting up value for table view for multi-selection row
- (id)initWithTitle:(NSString *)aTitle options:(NSArray *)aOptions matchingArray:(NSArray *)matchArray xy:(CGPoint)point size:(CGSize)size isMultiple:(BOOL)isMultiple
{
isMultipleSelection=isMultiple;
float height = MIN(size.height, DROPDOWNVIEW_HEADER_HEIGHT+[aOptions count]*44);
CGRect rect = CGRectMake(point.x, point.y, size.width, height);
if (self = [super initWithFrame:rect])
{
self.backgroundColor = [UIColor clearColor];
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOffset = CGSizeMake(2.5, 2.5);
self.layer.shadowRadius = 2.0f;
self.layer.shadowOpacity = 0.5f;
_kTitleText = [aTitle copy];
_kDropDownOption = #[#"India",#"Swaziland",#"Africa",#"Australlia",#"Pakistan",#"Srilanka",#"Mexico",#"United Kingdom",#"United States",#"Portugal"];
_kMatchingArray = #[#"United States",#"Swaziland"];
finalarray=[[NSMutableArray alloc]init];
for(int i = 0;i<[_kMatchingArray count];i++)
{
for(int j= 0;j<[_kDropDownOption count];j++)
{
if([[_kMatchingArray objectAtIndex:i] isEqualToString:[_kDropDownOption objectAtIndex:j]])
{
NSLog(#"%d",j);
NSString *str = [NSString stringWithFormat:#"%d",j];
[finalarray addObject:str];
}
else {
}
}
}
NSLog(#"finalArray:%#",finalarray);
// NSLog(#"%#",_kMatchingArray);
self.arryData=[[NSMutableArray alloc]init];
_kTableView = [[UITableView alloc] initWithFrame:CGRectMake(DROPDOWNVIEW_SCREENINSET,
DROPDOWNVIEW_SCREENINSET + DROPDOWNVIEW_HEADER_HEIGHT,
rect.size.width - 2 * DROPDOWNVIEW_SCREENINSET,
rect.size.height - 2 * DROPDOWNVIEW_SCREENINSET - DROPDOWNVIEW_HEADER_HEIGHT - RADIUS)];
_kTableView.separatorColor = [UIColor colorWithWhite:1 alpha:.2];
_kTableView.separatorInset = UIEdgeInsetsZero;
_kTableView.backgroundColor = [UIColor clearColor];
_kTableView.dataSource = self;
_kTableView.delegate = self;
[self addSubview:_kTableView];
if (isMultipleSelection) {
UIButton *btnDone=[UIButton buttonWithType:UIButtonTypeCustom];
[btnDone setFrame:CGRectMake(rect.origin.x+182,rect.origin.y-45, 82, 31)];
[btnDone setImage:[UIImage imageNamed:#"done#2x.png"] forState:UIControlStateNormal];
[btnDone addTarget:self action:#selector(Click_Done) forControlEvents: UIControlEventTouchUpInside];
[self addSubview:btnDone];
}
}
return self;
}
using this i have create a tableview fetching the values
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_kDropDownOption count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cel lForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentity = #"DropDownViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentity];
cell = [[DropDownViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity];
NSInteger row = [indexPath row];
// NSIndexPath *selectedIndexPath = [finalarray addObject:indexPath.row];
UIImageView *imgarrow=[[UIImageView alloc]init ];
NSLog(#"aray:%#",self.arryData);
if([self.arryData containsObject:indexPath]){
imgarrow.frame=CGRectMake(230,2, 27, 27);
imgarrow.image=[UIImage imageNamed:#"check_mark#2x.png"];
} else
imgarrow.image=nil;
[cell addSubview:imgarrow];
cell.textLabel.text = [_kDropDownOption objectAtIndex:row] ;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (isMultipleSelection) {
if([self.arryData containsObject:indexPath]){
[self.arryData removeObject:indexPath];
} else {
[self.arryData addObject:indexPath];
}
[tableView reloadData];
} else {
if (self.delegate && [self.delegate respondsToSelector:#selector(DropDownListView:didSelectedIndex:)]) {
[self.delegate DropDownListView:self didSelectedIndex:[indexPath row]];
}
// dismiss self
[self fadeOut];
}
}
I have two array one have total records of the tableview and another one have initially selected values.I have compare the two arrays and get matching indexpath. My problem was how to set check mark image on matched values row?
if ([_strProIDNS isEqualToString:strUNAssignNS])
{
tblViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
tblViewCell.accessoryType = UITableViewCellAccessoryNone;
My problem is that there is this ugly white space that is appearing at the bottom of my UITableView.
The only way to get the cell divider line thing (whatever it is called) is to scroll all the way to the bottom and back up, then it will appear.
But even when it appears, there is still a massive area of white space below my last cell.
Here is my two images to illustrate my problem.
Here is my code:
Post *currentPost;
#interface CommentViewController ()
#end
#implementation CommentViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor colorWithRed:(255/255.0) green:(221/255.0) blue:(85/255.0) alpha:1.0f]];
[self.leftLabel setBackgroundColor:[UIColor colorWithRed:(255/255.0) green:(221/255.0) blue:(85/255.0) alpha:1.0f]];
[self.rightLabel setBackgroundColor:[UIColor colorWithRed:(255/255.0) green:(221/255.0) blue:(85/255.0) alpha:1.0f]];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://kmjt.org/getComments.php"]];
NSData *data = [NSData dataWithContentsOfURL:url];
jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
postArray = [[NSMutableArray alloc] init];
for(int i = 0; i < jsonArray.count; i++)
{
NSString *nickname = [[jsonArray objectAtIndex:i] objectForKey:#"nickname"];
NSString *squeal = [[jsonArray objectAtIndex:i] objectForKey:#"comment"];
NSString *timeSincePosted = [[jsonArray objectAtIndex:i] objectForKey:#"TIME_TO_SEC(TIMEDIFF(NOW( ), c.timePosted))"];
[postArray addObject:[[Post alloc] initWithNickname:nickname andSqueal:squeal andTimeSincePosted:timeSincePosted]];
}
viewArray = [[NSMutableArray alloc] init];
UILabel *nicknameLabelMainPost = [[UILabel alloc]initWithFrame:CGRectMake(43, 5, 320-10, 30)];
nicknameLabelMainPost.backgroundColor = [UIColor clearColor];
nicknameLabelMainPost.text = currentPost.nickname;
nicknameLabelMainPost.font = [UIFont boldSystemFontOfSize:20];
UITextView *textViewMainPost = [[UITextView alloc] initWithFrame:CGRectMake(38, 22, 320-34, 0)];
textViewMainPost.backgroundColor = [UIColor clearColor];
textViewMainPost.font = [UIFont systemFontOfSize:15];
textViewMainPost.text = currentPost.squeal;
[textViewMainPost sizeToFit];
[textViewMainPost layoutIfNeeded];
textViewMainPost.userInteractionEnabled = NO;
UILabel *timePostedLabelMainPost = [[UILabel alloc]initWithFrame:CGRectMake(43, 50+textViewMainPost.contentSize.height-20, 320-10, 15)];
timePostedLabelMainPost.backgroundColor = [UIColor clearColor];
timePostedLabelMainPost.text = [self getTimeSincePostedLabel:currentPost.timeSincePosted];
timePostedLabelMainPost.textColor = [UIColor colorWithRed:(181/255.0) green:(181/255.0) blue:(189/255.0) alpha:1.0f];
timePostedLabelMainPost.font = [UIFont boldSystemFontOfSize:15];
UIView *view = [[UIView alloc] initWithFrame: CGRectMake (0, 0, 320, 30+textViewMainPost.frame.size.height)]; // 30 is nickname label height, 20 is time posted label height
[view addSubview:nicknameLabelMainPost];
[view addSubview:textViewMainPost];
[view addSubview:timePostedLabelMainPost];
[viewArray addObject:view];
for(int i = 0; i < postArray.count; i++)
{
Post *postObject;
postObject = [postArray objectAtIndex:i];
UILabel *nicknameLabel = [[UILabel alloc]initWithFrame:CGRectMake(75, 5, 320-65, 30)];
nicknameLabel.backgroundColor = [UIColor clearColor];
nicknameLabel.text = postObject.nickname;
nicknameLabel.font = [UIFont boldSystemFontOfSize:15];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(70, 22, 320-65, 0)];
textView.backgroundColor = [UIColor clearColor];
textView.font = [UIFont systemFontOfSize:10];
textView.text = postObject.squeal;
[textView sizeToFit];
[textView layoutIfNeeded];
textView.userInteractionEnabled = NO;
UILabel *timePostedLabel = [[UILabel alloc]initWithFrame:CGRectMake(75, 50+textView.contentSize.height-20, 320-65, 15)];
timePostedLabel.backgroundColor = [UIColor clearColor];
timePostedLabel.text = [self getTimeSincePostedLabel:postObject.timeSincePosted];
timePostedLabel.textColor = [UIColor colorWithRed:(181/255.0) green:(181/255.0) blue:(189/255.0) alpha:1.0f];
timePostedLabel.font = [UIFont boldSystemFontOfSize:15];
UIView *view = [[UIView alloc] initWithFrame: CGRectMake (0, 0, 320, 30+textView.frame.size.height+20-20+2)]; // 30 is nickname label height, 20 is time posted label height
[view addSubview:nicknameLabel];
[view addSubview:textView];
[view addSubview:timePostedLabel];
[viewArray addObject:view];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"%i", viewArray.count); // This is returning 3 (the correct amount of rows)
return viewArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
else
[[cell.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
[cell.contentView addSubview:[viewArray objectAtIndex:indexPath.row]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIView *view = [viewArray objectAtIndex:indexPath.row];
return view.frame.size.height+30;
}
- (NSString *)getTimeSincePostedLabel:(NSString *)secondsString
{
int num_seconds = [secondsString integerValue];
int days = num_seconds / (60 * 60 * 24);
num_seconds -= days * (60 * 60 * 24);
int hours = num_seconds / (60 * 60);
num_seconds -= hours * (60 * 60);
int minutes = num_seconds / 60;
if(days > 0)
return [NSString stringWithFormat:#"%i d", days];
else if(hours > 0)
return [NSString stringWithFormat:#"%i h", hours];
else if(minutes > 0)
return [NSString stringWithFormat:#"%i m", minutes];
return [NSString stringWithFormat:#"%i s", num_seconds];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
i think its the problem with your heightForRowAtIndexPath,every time you increasing view height by 30,try this
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIView *view = [viewArray objectAtIndex:indexPath.row];
CGFloat cellHeight = view.frame.size.height;
return cellHeight+30;
}
If i select some segments and then scroll the tableView down, it forgets my selected segments?
What am I do'ing wrong? Or what am I missing? It looks like it resets the cell every time, but I've tried to remove the cell = nil; but with no luck. All the code I think might have impact on this is here:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"FancyCell"];
cell = nil;
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"FancyCell"];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
// add the segmentedControl when you create a new cell
UIImage *correctImageGreen = [[UIImage imageNamed:#"green.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *correctImageGul = [[UIImage imageNamed:#"gul.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *correctImageRed = [[UIImage imageNamed:#"red.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
NSArray *itemArray = [NSArray arrayWithObjects: correctImageGreen, correctImageGul, correctImageRed, nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(308, 8, 150, 28);
[cell.contentView addSubview:segmentedControl];
// add an action so we can change our model if the view changes
[segmentedControl addTarget:self action:#selector(didChangeSegmentedControl:) forControlEvents:UIControlEventValueChanged];
// use a tag so we can retrieve the segmentedControl later
segmentedControl.tag = 42;
}
// either if the cell could be dequeued or you created a new cell,
// segmentedControl will contain a valid instance
UISegmentedControl *segmentedControl = (UISegmentedControl *)[cell.contentView viewWithTag:42];
UIImage *comment = [UIImage imageNamed:#"Checkmark-hvid"];
UIImage *imageRef = [UIImage imageNamed:#"Checkmark-hvid"];
UIImageView *commentView = [[UIImageView alloc] initWithImage: comment];
UIImageView *imageRefView = [[UIImageView alloc] initWithImage: imageRef];
commentView.frame = CGRectMake(625, 5, 30, 30);
imageRefView.frame = CGRectMake(515, 5, 30, 30);
commentView.tag = 98;
imageRefView.tag = 99;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
boolean_t didGetStates = [defaults boolForKey:#"didGetStates"];
if (didGetStates) {
NSDictionary *dic = [tableData objectAtIndex:indexPath.row];
int selectedState = [[dic valueForKey:#"State"] intValue];
[cell.contentView addSubview:segmentedControl];
if (selectedState == -1) {
segmentedControl.selectedSegmentIndex = -1;
}
else {
segmentedControl.selectedSegmentIndex = selectedState;
}
int comment = [[dic valueForKey:#"Comment"] intValue];
int imageRef = [[dic valueForKey:#"Foto"] intValue];
if (comment == 0) {
[cell.contentView addSubview:commentView];
}
else {
[[cell.contentView viewWithTag:98]removeFromSuperview];
}
if (imageRef == 0) {
[cell.contentView addSubview:imageRefView];
}
else {
[[cell.contentView viewWithTag:99]removeFromSuperview];
}
}
MBFancyObject *object = _objects[indexPath.row];
cell.textLabel.text = object.title;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
object.selectedIndex = segmentedControl.selectedSegmentIndex;
object.currentIndexRow = indexPath.row;
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
return cell;
}
The action that is called, when a segment is chosen..
- (IBAction)didChangeSegmentedControl:(UISegmentedControl *)sender {
// transform the origin of the cell to the frame of the tableView
CGPoint senderOriginInTableView = [self.tableView convertPoint:CGPointZero fromView:sender];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:senderOriginInTableView];
NSAssert(indexPath, #"must have a valid indexPath");
MBFancyObject *object = _objects[indexPath.row];
object.selectedIndex = sender.selectedSegmentIndex;
}
If you got any idea please write an answer or if you need more information tell me!! :)
THANK YOU!!!
Mikkel - The 15 year old iDeveloper! ;)
You are misunderstanding how table view cells work. They are not for saving state information, they are for presenting it, and interacting with the user.
When a table view cell is scrolled off-screen, the system tosses it into a recycle queue, and any field settings in the cell are lost.
When a user interacts with the controls in your table view, you should immediately save the changed information to your model (data storage). Then, when the table view asks you for a cell in the tableView:cellForRowAtIndexPath: method, you should create and FULLY configure a cell with the values from your model.
I found out that i forgot that i get data from my database, witch returns -1. And if I choose segment 2 and then scroll down and then back up, it show the value from the database :) This was fixable by adding a boolan to check where it should look fore the selected segment :) Now it looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"FancyCell"];
cell = nil;
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"FancyCell"];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
// add the segmentedControl when you create a new cell
UIImage *correctImageGreen = [[UIImage imageNamed:#"green.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *correctImageGul = [[UIImage imageNamed:#"gul.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *correctImageRed = [[UIImage imageNamed:#"red.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
NSArray *itemArray = [NSArray arrayWithObjects: correctImageGreen, correctImageGul, correctImageRed, nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(308, 8, 150, 28);
[cell.contentView addSubview:segmentedControl];
// add an action so we can change our model if the view changes
[segmentedControl addTarget:self action:#selector(didChangeSegmentedControl:) forControlEvents:UIControlEventValueChanged];
// use a tag so we can retrieve the segmentedControl later
segmentedControl.tag = 42;
}
// either if the cell could be dequeued or you created a new cell,
// segmentedControl will contain a valid instance
UISegmentedControl *segmentedControl = (UISegmentedControl *)[cell.contentView viewWithTag:42];
UIImage *comment = [UIImage imageNamed:#"Checkmark-hvid"];
UIImage *imageRef = [UIImage imageNamed:#"Checkmark-hvid"];
UIImageView *commentView = [[UIImageView alloc] initWithImage: comment];
UIImageView *imageRefView = [[UIImageView alloc] initWithImage: imageRef];
commentView.frame = CGRectMake(625, 5, 30, 30);
imageRefView.frame = CGRectMake(515, 5, 30, 30);
commentView.tag = 98;
imageRefView.tag = 99;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
boolean_t didGetStates = [defaults boolForKey:#"didGetStates"];
boolean_t didSelectState = [defaults boolForKey:#"didSelectState"];
MBFancyObject *object = _objects[indexPath.row];
if (didGetStates) {
NSDictionary *dic = [tableData objectAtIndex:indexPath.row];
if (didSelectState) {
segmentedControl.selectedSegmentIndex = object.selectedIndex;
}
else {
int selectedState = [[dic valueForKey:#"State"] intValue];
[cell.contentView addSubview:segmentedControl];
if (selectedState == -1) {
segmentedControl.selectedSegmentIndex = -1;
}
else {
segmentedControl.selectedSegmentIndex = selectedState;
}
}
int comment = [[dic valueForKey:#"Comment"] intValue];
int imageRef = [[dic valueForKey:#"Foto"] intValue];
if (comment == 0) {
[cell.contentView addSubview:commentView];
}
else {
[[cell.contentView viewWithTag:98]removeFromSuperview];
}
if (imageRef == 0) {
[cell.contentView addSubview:imageRefView];
}
else {
[[cell.contentView viewWithTag:99]removeFromSuperview];
}
}
cell.textLabel.text = object.title;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
object.selectedIndex = segmentedControl.selectedSegmentIndex;
object.currentIndexRow = indexPath.row;
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
return cell;
}
And the segmentedControl's action:
- (IBAction)didChangeSegmentedControl:(UISegmentedControl *)sender {
// transform the origin of the cell to the frame of the tableView
CGPoint senderOriginInTableView = [self.tableView convertPoint:CGPointZero fromView:sender];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:senderOriginInTableView];
NSAssert(indexPath, #"must have a valid indexPath");
MBFancyObject *object = _objects[indexPath.row];
object.selectedIndex = sender.selectedSegmentIndex;
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:#"didSelectState"];
}
Hi i am almost new in programing.
I am faced with an error that I couldn't solve anyway. Even after comparing with anther solutions.
I have worked on it for about 3 days.
So let me completely describe my problem:
1.this is my implementation code:
#import "DocumentTableViewController.h"
#import "AddDocumentTableView.h"
#import "DB_document.h"
#implementation DocumentTableViewController
#synthesize managedObjectContext;
#synthesize btnAddDocument;
#synthesize fetchedObjects;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
NSManagedObjectContext *context = managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"DB_document" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSError *error;
fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
NSLog(#"%d",[fetchedObjects count]);
}
- (void)viewDidUnload
{
[self setBtnAddDocument:nil];
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int result = 0;
if (section == 0)
{
result = [fetchedObjects count] + 1;
}
else if (section == 1)
{
result = 1;
}
return result;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
DB_document *db_document = [fetchedObjects objectAtIndex:indexPath.row];
if (indexPath.section == 0 && indexPath.row == 0)
{
UILabel *lblMoney = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 70, 40)];
lblMoney.text = #"amount";
lblMoney.textAlignment = UITextAlignmentCenter;
lblMoney.textColor = [UIColor blackColor];
lblMoney.backgroundColor = [UIColor clearColor];
lblMoney.font = [UIFont systemFontOfSize:12];
[cell addSubview:lblMoney];
UILabel *lblDescription = [[UILabel alloc] initWithFrame:CGRectMake(85, 0, 150, 40)];
lblDescription.text = #"description";
lblDescription.textAlignment = UITextAlignmentCenter;
lblDescription.textColor = [UIColor blackColor];
lblDescription.backgroundColor = [UIColor clearColor];
lblDescription.font = [UIFont systemFontOfSize:12];
[cell addSubview:lblDescription];
UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 70, 40)];
lblDate.text = #"date";
lblDate.textAlignment = UITextAlignmentCenter;
lblDate.textColor = [UIColor blackColor];
lblDate.backgroundColor = [UIColor clearColor];
lblDate.font = [UIFont systemFontOfSize:12];
[cell addSubview:lblDate];
UIButton *btnLine1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnLine1.frame = CGRectMake(80, 0, 1, 40);
[cell addSubview:btnLine1];
UIButton *btnLine2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnLine2.frame = CGRectMake(240, 0, 1, 40);
[cell addSubview:btnLine2];
return cell;
}
if (indexPath.section == 0 && indexPath.row != 0)
{
UILabel *lblMoney = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 70, 40)];
lblMoney.text = [NSString stringWithFormat:#"%d",db_document.docAmount];
lblMoney.textAlignment = UITextAlignmentCenter;
lblMoney.textColor = [UIColor blackColor];
lblMoney.backgroundColor = [UIColor clearColor];
lblMoney.font = [UIFont systemFontOfSize:12];
[cell addSubview:lblMoney];
UILabel *lblDescription = [[UILabel alloc] initWithFrame:CGRectMake(85, 0, 150, 40)];
lblDescription.text = db_document.docDescription;
lblDescription.numberOfLines = 2;
lblDescription.textAlignment = UITextAlignmentCenter;
lblDescription.textColor = [UIColor blackColor];
lblDescription.backgroundColor = [UIColor clearColor];
lblDescription.font = [UIFont systemFontOfSize:12];
[cell addSubview:lblDescription];
UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 70, 40)];
NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];
[dateFormater setDateFormat:#"yyyy/mm/dd"];
lblDate.text = [NSString stringWithFormat:#"%#",[dateFormater stringFromDate:(NSDate *)db_document.docDate]];
lblDate.textAlignment = UITextAlignmentCenter;
lblDate.textColor = [UIColor blackColor];
lblDate.backgroundColor = [UIColor clearColor];
lblDate.font = [UIFont systemFontOfSize:12];
[cell addSubview:lblDate];
UIButton *btnLine1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnLine1.frame = CGRectMake(80, 0, 1, 40);
[cell addSubview:btnLine1];
UIButton *btnLine2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnLine2.frame = CGRectMake(240, 0, 1, 40);
[cell addSubview:btnLine2];
return cell;
}
if (indexPath.section == 1)
{
UILabel *lblMoney = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 70, 40)];
lblMoney.text = #"";
lblMoney.textAlignment = UITextAlignmentCenter;
lblMoney.textColor = [UIColor blackColor];
lblMoney.backgroundColor = [UIColor clearColor];
lblMoney.font = [UIFont systemFontOfSize:12];
[cell addSubview:lblMoney];
UILabel *lblTotalAmount = [[UILabel alloc] initWithFrame:CGRectMake(165, 0, 140, 40)];
lblTotalAmount.text = #"amounts";
lblTotalAmount.textAlignment = UITextAlignmentCenter;
lblTotalAmount.textColor = [UIColor blackColor];
lblTotalAmount.backgroundColor = [UIColor clearColor];
lblTotalAmount.font = [UIFont systemFontOfSize:12];
[cell addSubview:lblTotalAmount];
UIButton *btnLine = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnLine.frame = CGRectMake(160, 0, 1, 40);
[cell addSubview:btnLine];
return cell;
}
return cell;
}
- (IBAction)btnAddDocument_click:(id)sender
{
AddDocumentTableView *addDocumentTableView = [[AddDocumentTableView alloc] init];
addDocumentTableView.managedObjectContext = managedObjectContext;
[self.navigationController pushViewController:addDocumentTableView animated:YES];
}
2.this is the error:
2012-06-16 15:25:31.696 Account5[5534:fb03] 2
2012-06-16 15:25:31.704 Account5[5534:fb03] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[_PFArray objectAtIndex:]: index (2) beyond bounds (2)'
*** First throw call stack:
3.Let me describe the program. I can save data to data base with core data but when i want to fetch the data program jumps out.I have to consider that I think NSManagedObjectContext fetched data because fetchedObjects Array has 2 data because I inserted in.
and I have to say that my RootViewController is DocumentTableViewController it means that exactly when I run the program it crashes. And if I want to run the app I must comment DB_document *db_document = [fetchedObjects objectAtIndex:indexPath.row];
and after that app runs and I can insert data in another page.
I have to consider that when the app crashes it stops exactly on
DB_document *db_document = [fetchedObjects objectAtIndex:indexPath.row];
with green highlighted line.
thank you
Here is your problem:
In numberOfRowsInSection you add one to the fetchedResults array. Presumably, you want to add an additional row in that section. That is fine.
However, in cellForRowAtIndexPath you take indexPath.row as the index for your db_document. Obviously, in the last row it will crash. You have to first check in which row you are and then retrieve your db_document only if you need it for that particular row.
Problem is here :
result = [fetchedObjects count] + 1;
on numberOfRowsInSection
you add one row more than fetchedObjects count and when you on the cellForRowAtIndexPath
the fetchedObjects has for example 3 row and you get row 4 ,and this exception occored and you get out of range on this array.
I'm a newbie iOS developer writing my apps in Objective-C language. I found myself facing a problem, and I searched a lot, but without results :(
The problem is: I'm programmatically populating uitable view cells with an image, an "eyelet" and a title, parsed from a news xml grabbed online.
The code I'm going to past here under make the UI snaps, but I already found the cause and I'm working on the solution, so no problem about this, the fact is that this code load the contents, but when I try to open a second view controller with a news detail I previously made...well...the cells aren't clickable!
Here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *eyeletLabel, *newsTitleLabel, *dateLabel;
eyeletLabel = [UILabel new];
newsTitleLabel = [UILabel new];
newsImage = [UIImageView new];
if (cell == nil) {
//NSLog(#"Cell: %#", cell);
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
ATNews *thisNews = gNewsArray[indexPath.row];
eyeletLabel.numberOfLines = 0;
eyeletLabel.lineBreakMode = NSLineBreakByWordWrapping;
eyeletLabel.frame = CGRectMake(90.0f, 5.0f, cell.contentView.frame.size.width - 20, 20);
eyeletLabel.text = thisNews.eyelet;
eyeletLabel.textColor = [UIColor orangeColor];
newsTitleLabel.numberOfLines = 0;
newsTitleLabel.lineBreakMode = NSLineBreakByWordWrapping;
newsTitleLabel.frame = CGRectMake(90.0f, 20.0f, cell.contentView.frame.size.width - 50, 50);
newsTitleLabel.text = thisNews.title;
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSString *newDate = [formatter stringFromDate:thisNews.pubDate];
dateLabel.text = [NSString stringWithString:newDate];
for (id obj in thisNews.newsPhotos) {
NSUInteger counter = 0;
if ([obj isEqualToString: #"true"]) {
counter = counter + 2;
imageUrlFromArray = [thisNews.newsPhotos objectAtIndex:counter];
NSURL *imageURL = [NSURL URLWithString:imageUrlFromArray];
imageData = [NSData dataWithContentsOfURL:imageURL];
}
counter = counter + 3;
}
newsImage.frame = CGRectMake(5.0f, 5.0f, 70.0f, 70.0f);
if (thisNews.newsPhotos.count != 0) {
newsImage.image = nil;
newsImage.image = [UIImage imageWithData:imageData];
newsImage.contentMode = UIViewContentModeScaleAspectFill;
newsImage.layer.cornerRadius = newsImage.frame.size.width / 2;
[newsImage.layer setMasksToBounds:YES];
}
else {
newsImage.image = [UIImage imageNamed:#"no-image.png"];
newsImage.contentMode = UIViewContentModeScaleAspectFit;
newsImage.layer.cornerRadius = newsImage.frame.size.width / 2;
[newsImage.layer setMasksToBounds:YES];
}
[cell addSubview:newsImage];
[cell addSubview:eyeletLabel];
[cell addSubview:newsTitleLabel];
[cell addSubview:dateLabel];
}
else {
//NSLog(#"Cell");
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
}
return cell;
}
I searched for the solution a lot, I also read some similar question in here too, but without success, could you help me? Thank you very much!
Implement the delegate method
- (void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath;
Now that it is clickable, you need to either add a segue from your cell to your new view controller or from your view controller create a segue to the new view controller and call
[self.viewcontroller performSegueWithIdentifier:#"Your Identifer"];