UITableView row order - ios

I have a strange behavior my tableView
I make dropdown section on my tableView. It's look like:
but if I reopen my section rows order was ather like this:
- (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];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
NSLog(#"CURRENT ROW = %ld", (long)indexPath.row);
UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(90, 20, cell.frame.size.width - 90, 20)];
titleLable.text = [NSString stringWithFormat:#"урок %ld", (long)indexPath.row];
[cell addSubview:titleLable];
UILabel *percentLable = [[UILabel alloc] initWithFrame:CGRectMake(cell.frame.size.width - 40, 20, 50, 20)];
LessonModel *lesson = [self.lessonsArray objectAtIndex:indexPath.section];
NSArray *childrenArrey = [[NSArray alloc] initWithArray:(NSArray *) lesson.childrenArray];;
percentLable.text =[NSString stringWithFormat:#"%# %%", [childrenArrey[indexPath.row] valueForKey:#"percent"]];
[cell addSubview:percentLable];
}
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
LessonModel *lesson = [self.lessonsArray objectAtIndex:indexPath.section];
NSArray *childrenArrey = [[NSArray alloc] initWithArray:(NSArray *) lesson.childrenArray];
NSString *uid = [childrenArrey[indexPath.row] valueForKey:#"id"];
[self makeLessonWithId:uid];
}
- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
if (indexPath.row == 0) {
BOOL collapsed = [[self.arrayForBool objectAtIndex:indexPath.section] boolValue];
collapsed = !collapsed;
[self.arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:collapsed]];
[self updateLabelFrame:gestureRecognizer.view.tag];
NSRange range = NSMakeRange(indexPath.section, 1);
NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
[self.tableView reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade];
}
}

Try by putting values in else part too:
if (cell == nil){
//your code as it is
}
else{
titleLable.text = [NSString stringWithFormat:#"урок %ld", (long)indexPath.row];
NSArray *childrenArrey = [[NSArray alloc] initWithArray:(NSArray *) lesson.childrenArray];;
percentLable.text =[NSString stringWithFormat:#"%# %%", [childrenArrey[indexPath.row] valueForKey:#"percent"]];
}

Related

how to set value from picker to customized cell textfield of particular section?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section==0)
{
if(indexPath.row==1)
{
static NSString *CellIdentifier = #"custom1TableViewCell";
custom1TableViewCell *cell2 = (custom1TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell2 == nil)
{
cell2 = [[ custom1TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *dictionary = [dataArray objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"data"];
cell2.language.text = [array objectAtIndex:indexPath.row];
//for picker
[cell2.sellanguage setInputView:pickerview];
cell2.sellanguage.text = selectedLanguage;
return cell2;
}
static NSString *CellIdentifier = #"customTableViewCell";
customTableViewCell *cell = (customTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[customTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *dictionary = [dataArray objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"data"];
cell.soundfx.text = [array objectAtIndex:indexPath.row];
return cell;
}
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
[pickerview selectRow:row inComponent:0 animated:YES];
NSLog(#"%#",[languages objectAtIndex:row]);
selectedLanguage= [languages objectAtIndex:row];
[[self view] endEditing:YES];
[self.tableview reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// now you can use cell.textLabel.text
//For Example
self.textfield.text = cell.textLabel.text;
}
I hope this clears your doubt.

Objective-C: Change UIImageView in Custom Cell change different cell images

I am using a custom cell on UITableView with multi Section and i’m added a UITapGestureRecognizer to a UIImageView for change it when the image is selected but When I select the image other's images in different cell are selected as well.
This is the code for the TableView datasource method cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"FilterCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
UILabel *text = (UILabel *)[cell viewWithTag:ROW_TITLE];
UIImageView *image = (UIImageView *)[cell viewWithTag:ROW_IMAGE];
NSDictionary *item = [results objectAtIndex:indexPath.section];
NSString *string = [item objectForKey:JSONResp_common_name];
[text setText:((string != [NSNull null])? string : #"")];
if ([[item objectForKey:JSONResp_common_status] isEqualToNumber:[NSNumber numberWithBool:YES]])
[image setImage:[UIImage imageNamed:#"checked"]];
else
[image setImage:[UIImage imageNamed:#"unchecked"]];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
[image setTag:indexPath.row];
[image setUserInteractionEnabled:YES]; action:#selector(handleTapFrom:)];
if([self tableView:tableView
canCollapseSection:indexPath.section]){ // EXPANDABLE ROW
if(!indexPath.row){// HEADER
// SET ACCESORIES
if([expandedSections containsIndex:indexPath.section])
[cell setAccessoryView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"menos"]]];
else
[cell setAccessoryView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"mas"]]];
[image setUserInteractionEnabled:NO];
//Remove checked option from a cell
for(UITapGestureRecognizer *tapGesture in cell.gestureRecognizers)
[cell removeGestureRecognizer:tapGesture];
}else{// SUB ROWS
NSArray *subValues = [item objectForKey:PARAM_STRUCT_SUBELEMENTS];
NSDictionary *detail = [subValues objectAtIndex:indexPath.row -1];
//REASING VALUES TO SUB VALUES
if(detail && [detail isKindOfClass:[City class]]){
City *currentCity = (City *)detail;
[text setText:[currentCity name]];
}
[cell setAccessoryView:nil];
[tapGestureRecognizer setDelegate:self];
[cell addGestureRecognizer:tapGestureRecognizer];
}
}else{//ROWS CHECKED OPTION
[cell setAccessoryView:nil];
[tapGestureRecognizer setDelegate:self];
[cell addGestureRecognizer:tapGestureRecognizer];
}
return cell;
}
</code>
</pre>
This is the code for the TableView datasource method didSelectRowAtIndexPath:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *data = [self.results objectAtIndex:indexPath.section];
if([self tableView:tableView
canCollapseSection:indexPath.section]){
if(!indexPath.row){// EXPANDABLE
// only first row toggles expandaed/collapse
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSInteger section = indexPath.section;
BOOL currentlyExpanded = [expandedSections containsIndex:section];
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];
if(currentlyExpanded){
rows = [self tableView:tableView
numberOfRowsInSection:section];
[expandedSections removeIndex:section];
}else{
[expandedSections addIndex:section];
rows = [self tableView:tableView
numberOfRowsInSection:section];
}
for(int i = 1; i < rows; i++){
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:section];
[tmpArray addObject:tmpIndexPath];
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(currentlyExpanded){
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"mas"]];
}else{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"menos"]];
}
}else{// Detail
}
}else{ // Normal Row
//[]
}}
This is the code for the TableView datasource method fro the UITapGestureRecognizer
- (void)handleTapFrom:(UITapGestureRecognizer *)recognizer {
UITableViewCell *cellView = (UITableViewCell *)[recognizer view];
UIImageView *switchImageView = nil;
for (UIView *item in [[[cellView subviews] firstObject] subviews]) {
if ([item isKindOfClass:[UIImageView class]] && ((UIImageView *)item).image != nil) {
switchImageView = (UIImageView *)item;
break;
}
}
if (switchImageView == nil) {
for (UIView *item in [[[[[cellView subviews] firstObject] subviews] objectAtIndex:1] subviews]) {
if ([item isKindOfClass:[UIImageView class]] && ((UIImageView *)item).image != nil) {
switchImageView = (UIImageView *)item;
break;
}
}
}
NSDictionary *item = [results objectAtIndex:switchImageView.tag];
if ([Utility image:switchImageView.image
isEqualTo:[UIImage imageNamed:IMAGE_UNCHECKED]]) {
if ([[item objectForKey:JSONResp_common_action] isEqualToString:JSONResp_sortings]) {
[delegate didCheckFilter:item
withStatus:YES];
} else if ([[item objectForKey:JSONResp_common_action] isEqualToString:JSONResp_filters]) {
[switchImageView setImage:[UIImage imageNamed:IMAGE_CHECKED]]; //change to a selected image
[delegate didCheckFilter:[item objectForKey:JSONResp_common_type]
withStatus:YES];
}
} else {
if ([[item objectForKey:JSONResp_common_action] isEqualToString:JSONResp_filters]) {
[switchImageView setImage:[UIImage imageNamed:IMAGE_UNCHECKED]];
[delegate didCheckFilter:[item objectForKey:JSONResp_common_type]
withStatus:NO];
}
}}
While declaring the UITapGestureRecognizer on particular cell assign the tag as
cell.tag = indexPath.row;
[cell addGestureRecognizer:tapGestureRecognizer];
and in your handler do like this
- (void)handleTapFrom:(UITapGestureRecognizer *)recognizer {
UITableViewCell *cellView = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:recognizer.tag inSection:0]];
// your stuff
}
Hope this Helps!

Expand UITableViewCell with smooth animation

I am trying to Animate the transition of a UITableViewCell when its height is changed. For this I am using the following lines of code:
[meetingsTable beginUpdates];
[meetingsTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:changedRow inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
[meetingsTable endUpdates];
but it is not showing the row height change animation. It just displays the expanded cell. However, if I remove the line
[meetingsTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:changedRow inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
it shows the expected animated height change but the cell is not reloaded. I have tried all the available UITableViewRowAnimationoptions but with no success. I have also tried
[meetingsTable reloadData];
but it is also not helpful.
Please suggest what can be the possible solution.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BMeetingsTableCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:#"MEETINGS"];
if (!cell) {
cell = [[BMeetingsTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MEETINGS"];
}
[self customizeCell:cell atIndexPath:indexPath forTable:tableView];
return cell;
}
- (void)customizeCell:(BMeetingsTableCell *)cell atIndexPath:(NSIndexPath *)indexPath forTable:(UITableView*)tableView shouldReload:(BOOL)shouldReload{
NSDictionary *event;
if (isSearching) {
event = [NSDictionary dictionaryWithDictionary:[searchedData objectAtIndex:indexPath.row]];
}else if ([tableView isEqual:_activeMeetingsTable])
event = [NSDictionary dictionaryWithDictionary:[activeEvents objectAtIndex:indexPath.row]];
else if ([tableView isEqual:_completedMeetingTable])
event = [NSDictionary dictionaryWithDictionary:[completedEvents objectAtIndex:indexPath.row]];
NSArray *partArr;
UILabel *showTimeLbl = (UILabel *)[cell viewWithTag:9];
UIImageView *showNewMeetingIcon = (UIImageView *)[cell viewWithTag:8];
[showTimeLbl setHidden:YES];
[showNewMeetingIcon setHidden:YES];
cell.delegate = self;
cell.tag = indexPath.row;
NSMutableArray* foundConts = [[NSMutableArray alloc]init];
NSMutableArray *tempPartArr = [[NSMutableArray alloc]init];
for (NSDictionary *dict in [event valueForKey:#"users"]) {
[tempPartArr addObject:dict];
}
partArr = [NSArray arrayWithArray:tempPartArr];
if ([displayName length]==0) displayName = numberString;
NSDictionary *participant = [[NSDictionary alloc]initWithObjectsAndKeys:inviteStat, #"inviteStatus", displayName, #"displayName", nil];
[foundConts addObject:participant];
}
}
if ([allParticipants count]> indexPath.row) {
[allParticipants replaceObjectAtIndex:indexPath.row withObject:invitedConts];
}else
[allParticipants addObject:invitedConts];
if ([tableView isEqual:_meetingsTable] && selectedRow == indexPath.row) {
cell.participants = [NSArray arrayWithArray:foundConts];
[cell.contsTable setHidden:NO];
// [cell.arrowButton setSelected:YES];
[cell.userImage setImage:[UIImage imageNamed:#"expandedProfilePic.png"]];
}else{
[cell.userImage setImage:[UIImage imageNamed:#"ProfileImage.png"]];
[cell.contsTable setHidden:YES];
// [cell.arrowButton setSelected:NO];
}
[cell.tapProfileBtn addTarget:self action:#selector(expandDetails:) forControlEvents:UIControlEventTouchUpInside];
cell.tapProfileBtn.tag = indexPath.row;
[cell.title setText:[event valueForKey:#"subject"]];
NSString* location;
if ([[event valueForKey:#"address"] isKindOfClass:[NSNull class]]) {
location = #"";
}else
location = [event valueForKey:#"location"];
long long startDateMilliSec = [[event valueForKey:#"start_time_milliseconds"] longLongValue];
NSDate *capturedStartDate = [NSDate dateWithTimeIntervalSince1970:startDateMilliSec];
//
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
NSDateComponents *comps1 = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:capturedStartDate];
NSString *theYear = [NSString stringWithFormat:#", %ld", (long)comps1.year];
[dateFormatter setDoesRelativeDateFormatting:YES];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSString *dateString = [dateFormatter stringFromDate:capturedStartDate];
if ([dateString isEqualToString:#"Today"]||[dateString isEqualToString:#"Tomorrow"]) {
}else{
[dateFormatter setDateFormat:#"dd MMMM yyyy"];
[dateFormatter setDoesRelativeDateFormatting:NO];
dateString = [dateFormatter stringFromDate:capturedStartDate];
}
NSString *amPM = (comps1.hour>=12)?#"PM":#"AM";
NSString *hourStr = [NSString stringWithFormat:#"%02d",(comps1.hour%12)];
dateString = [dateString stringByReplacingOccurrencesOfString:theYear withString:#""];
NSString *timeString = [NSString stringWithFormat:#"%#:%02d %#",hourStr, comps1.minute, amPM];
NSString *displayString = [NSString stringWithFormat:#"%# | %#",dateString, timeString];
[cell.date setText:[NSString stringWithFormat:#"%# | %#", displayString, location]];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == selectedRow) {
NSDictionary* event = [activeEvents objectAtIndex:indexPath.row];
NSArray *contacts = [event valueForKey:#"users"];
int height = ([contacts count]*50)+70;
return height;
}
else return 50
}
- (IBAction)expandDetails:(id)sender{
UIButton *btn = (UIButton*)sender;
selectedRow = btn.tag;
BMeetingsTableCell *changedCell = (BMeetingsTableCell*)[_activeMeetingsTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:btn.tag inSection:0]];
[self customizeCell:changedCell atIndexPath:[NSIndexPath indexPathForRow:btn.tag inSection:0] forTable:_meetingsTable];
[_meetingsTable beginUpdates];
[_meetingsTable endUpdates];
}
You have to update the cell without reloading the table view.
I imagine you have your cell customization code in cellForRowAtIndexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//dequeue cell
//customize cell at index path
return cell;
}
Move your customization into its own method that takes a cell.
- (void)customizeCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
//customize cell
}
Now when you need to update a cell you can do this...
- (void)rowWasChanged:(NSInteger)changedRow {
NSIndexPath *changedIndex = [NSIndexPath indexPathForRow:changedRow inSection:0];
UITableViewCell *changedCell = [self.meetingsTable cellForRowAtIndexPath:changedIndex];
[self customizeCell:changedCell atIndexPath:changedIndex];
[self.meetingsTable beginUpdates];
[self.meetingsTable endUpdates];
}
// Cell animation
if(sender.tag == 0) {
if(sender.on){
rowHeight1 = 250;
}else{
rowHeight1 = 55;
}
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
NSArray* rowsToReload = [NSArray arrayWithObjects:indexPath, nil];
[tblHealthQuestion reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationAutomatic];
}
// for Section animation
NSRange range = NSMakeRange(senctionIndex, 1);//NSMakeRange(0, 1);
NSIndexSet *section = [NSIndexSet indexSetWithIndexesInRange:range];
[tblHealthQuestion reloadSections:section withRowAnimation:UITableViewRowAnimationAutomatic];
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation{
[self reloadSections:sections withRowAnimation:animation];
}

Not inserting values probably in dynamically UITableView

i'm trying to create a dynamically fixtures which automatic updates using an API. The problem is it does connect the right sections with the right fixtures and the fixtures seem to duplicate in the different sections. Why is it not connecting the section date with the fixtures?
First i have an fixture array which contain all the fixtures.
[fixtures addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:date,#"date", time, #"time", competition_id,#"competition_id", home, #"home", away, #"away", nil]];
Then i have an array with contain all the dates with no duplicates ordered by date called reversedArray:
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"self" ascending:NO];
NSArray *descriptors = [NSArray arrayWithObject: descriptor];
NSArray *reverseOrder = [dateArray sortedArrayUsingDescriptors:descriptors];
reversedArray = [[reverseOrder reverseObjectEnumerator] allObjects];
Then i have my view where i look
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 22)];
/* Create custom view to display section header... */
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, tableView.frame.size.width, 22)];
[label setFont:[UIFont boldSystemFontOfSize:14]];
[label setTextColor:[UIColor whiteColor]];
NSString *dateString = [NSString stringWithFormat:#"%#",[reversedArray objectAtIndex:section]];
NSString *newString = [dateString substringToIndex:[dateString length]-15];
NSString *string =[NSString stringWithFormat:#"%#",newString];
[label setText:string];
[view addSubview:label];
[view setBackgroundColor:[UIColor colorWithRed:13/255.0f green:78/255.0f blue:102/255.0f alpha:1.0f]];
return view;
}
The numberOfRowsInSection method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *dateString = [NSString stringWithFormat:#"%#",[reversedArray objectAtIndex:section]];
NSLog(#"%# = %#", [dateString substringToIndex:[dateString length]-15], [[fixtures objectAtIndex:section] valueForKey:#"date"]);
NSArray *filteredArray = [fixtures filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"date == %#",[dateString substringToIndex:[dateString length]-15]]];
return filteredArray.count;
}
cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FixtureCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = [[FixtureCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
cell.homeTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"home" ];
cell.awayTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"away" ];
}
take out this code from your if sentence
cell.homeTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"home" ];
cell.awayTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"away" ];
you are reusing cells so the condition cell=nil will no be true always.
Also use indexPath.section for to know which data you should take for fill your cell
Assign label values outside if (cell == nil) reuse cell Identifier not enter the loop after cell creation
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FixtureCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = [[FixtureCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}
cell.homeTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"home" ];
cell.awayTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"away" ];
return cell;
}
Change this method cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FixtureCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil)
{
cell = [[FixtureCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}
cell.homeTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"home"];
cell.awayTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"away"];
return cell;
}
If you are using custom cell then try this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"FixtureCell";
FixtureCell *cell = (FixtureCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"FixtureCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.homeTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"home"];
cell.awayTeamLabel.text = [[fixtures objectAtIndex:indexPath.row] objectForKey:#"away"];
return cell;
}

tableView and how to correctly use "Load more..."

I'm have kind of a really small and annoying problem. (No pun intended)
I kind seem to find my mistake. I also asked in chat, but we couldn't find the solution. I got the "Load more..." for my tableView from Abizerns github and implemented it. It works, except for the part, that when it starts up I have to click "Load more...". So on start it doesn't show me the first entries of the tableView until I clicked on "Load more...". You can see my annoyance. Maybe someone can spot my mistake, so that the tableView actually loads the first ten entries on start up.
#define kNumberOfItemsToAdd 10
#define PADDING 10.0f
#interface Main ()
#end
#implementation Main
#synthesize tabelle;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
numberOfItemsToDisplay = kNumberOfItemsToAdd;
return self;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (numberOfItemsToDisplay == [tabelle count]) {
return 1;
}
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return numberOfItemsToDisplay;
} else {
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 6;
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.numberOfLines = 1;
}
if (indexPath.section == 0) {
cell.textLabel.font = [UIFont fontWithName:#"Verdana" size:14.0];
cell.detailTextLabel.font = [UIFont fontWithName:#"Verdana" size:12.0];
NSString *TableText = [[NSString alloc] initWithFormat:#"%#", [[tabelle objectAtIndex:indexPath.row] Name]];
NSString *TableText2 = [[NSString alloc] initWithFormat:#"%#", [[tabelle objectAtIndex:indexPath.row] m_m]];
NSString *cellValue = [NSString stringWithFormat:#"%#", TableText2];
NSString *cellValue2 = [NSString stringWithFormat:#"by: %#", TableText];
cell.textLabel.text = cellValue;
cell.detailTextLabel.textColor = [UIColor colorWithRed:0.196f green:0.3098f blue:0.52f alpha:1.f];
cell.detailTextLabel.text = cellValue2;
} else {
cell.textLabel.text = [NSString stringWithFormat:#"Next %d items", kNumberOfItemsToAdd];
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.textLabel.textColor = [UIColor colorWithRed:0.196f green:0.3098f blue:0.52f alpha:1.f];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14.f];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [[self.tabelle objectAtIndex:indexPath.row] m_m];
CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 3, 1000.0f)];
return textSize.height + PADDING * 3;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1) {
NSUInteger i, totalNumberOfItems = [tabelle count];
NSUInteger newNumberOfItemsToDisplay = MIN(totalNumberOfItems, numberOfItemsToDisplay + kNumberOfItemsToAdd);
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
for (i=numberOfItemsToDisplay; i<newNumberOfItemsToDisplay; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
numberOfItemsToDisplay = newNumberOfItemsToDisplay;
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
if (numberOfItemsToDisplay == totalNumberOfItems) {
[tableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationTop];
}
[tableView endUpdates];
// Scroll the cell to the top of the table
if (newNumberOfItemsToDisplay < totalNumberOfItems) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 200000000), dispatch_get_main_queue(), ^(void){
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
});
[tableView deselectRowAtIndexPath:indexPath animated:YES];
} else {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 200000000), dispatch_get_main_queue(), ^(void){
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:totalNumberOfItems-1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
});
}
}
}
I haven't used a "Load more..." implementation, but maybe what you need is add [self.tabelle reloadData] in your - (void)viewWillAppear:(BOOL)animated method.
What method gets called when you click "Load more..."? Can you not programmatically call that method when the tableview is loaded?

Resources