Objective-C : Always display 3 rows in UITableView - objective-c

After scrolling i want my TableView to display like this :
not like this :
and i have set my rowHeight = tableViewHeight / 3:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return CGRectGetHeight(self.tableView.frame)/3;
}

There are a couple things you need for this. Firstly, you need to detect when the table view has stopped scrolling:
How to know exactly when a UIScrollView's scrolling has stopped?
Next, you need to set the scroll view position:
How can I set the scrolling position of an UIScrollView?
The position you're going to want to set it might be the tableview contentOffset - (contentOffset % rowHeight) to move it up to show the row partially shown at the top.

Follow this steps:
In .h , declare
NSMutableArray *arrayForBool;
NSMutableArray *arrPastOrder;
In .m ,
- (void)viewDidLoad {
arrPastOrder=[[NSMutableArray alloc] init];
int range=1+arc4random()%10;
for(int i=0;i<range;i++)
{
NSMutableArray *arrinside=[[NSMutableArray alloc] init];
for(int j=0;j<3;j++)
{
if(j==0)
[arrinside addObject:[NSString stringWithFormat:#"Some Header %i",i]];
else
[arrinside addObject:[NSString stringWithFormat:#"Subindex %i",j]];
}
[arrPastOrder addObject:arrinside];
}
arrayForBool=[[NSMutableArray alloc]init];
for (int i=0; i<[arrPastOrder count]; i++)
{
[arrayForBool addObject:[NSNumber numberWithBool:YES]];
}
}
#pragma mark -
#pragma mark TableView DataSource and Delegate Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([[arrayForBool objectAtIndex:section] boolValue])
{
return [[arrPastOrder objectAtIndex:section] count]-1;
}
else
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *cellid=#"hello";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid];
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
}
BOOL manyCells = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
/********** If the section supposed to be closed *******************/
if(!manyCells)
{
cell.backgroundColor=[UIColor clearColor];
NSLog(#"cellForRowAtIndexPath---if");
cell.textLabel.text=#"";
}
/********** If the section supposed to be Opened *******************/
else
{
//cell.textLabel.text=[NSString stringWithFormat:#"%# %ld",[sectionTitleArray objectAtIndex:indexPath.section],indexPath.row+1];
// ic_keyboard_arrow_up_48pt_2x ic_keyboard_arrow_down_48pt_2x.png
cell.textLabel.text=[NSString stringWithFormat:#"%#",[[arrPastOrder objectAtIndex:indexPath.section] objectAtIndex:indexPath.row+1]];
}
cell.textLabel.textColor=[UIColor blackColor];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [arrPastOrder count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%li--%li",(long)indexPath.section, (long)indexPath.row);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[arrayForBool objectAtIndex:indexPath.section] boolValue]) {
return 40;
}
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 100;
}
#pragma mark - Creating View for TableView Section
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *sectionView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 540,100)];
sectionView.backgroundColor=[UIColor whiteColor];
sectionView.tag=section;
UILabel *viewLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 30, 530, 40)];
viewLabel.backgroundColor=[UIColor whiteColor];
viewLabel.textColor=[UIColor blackColor];
viewLabel.font=[UIFont boldSystemFontOfSize:20];
viewLabel.text=[NSString stringWithFormat:#"%#",[[arrPastOrder objectAtIndex:section] objectAtIndex:0]];
[sectionView addSubview:viewLabel];
/********** Add UITapGestureRecognizer to SectionView **************/
UITapGestureRecognizer *headerTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(sectionHeaderTapped:)];
[sectionView addGestureRecognizer:headerTapped];
return sectionView;
}
#pragma mark - Table header gesture tapped
- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
//Get which section is open, from here Set the value of arrafool. and expand collapse. Normally whole table is expanded.
}
Just run it. You will get the same structure. happy Coding !!

Related

Remove extra section or extra bottom space of tableview

I had create one simple tableview with section in which I was facing one issue that is, after displaying all section properly there are several dummy sections which I want to remove but reason why they had being displayed I couldn't able to found.
Here array of section count and row count under each section is proper.
So can you guys suggest how to remove that extra space and can obtain original table content size?
Below is code I done in my .m file specifically for tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if (searchEnabled)
{
return 1;
}
else
{
return tableDataArray.count;
}
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *headerView;
[headerView setBackgroundColor:[UIColor colorWithRed:216.0/255.0 green:216.0/255.0 blue:216.0/255.0 alpha:0.5]];
for (int i=0; i<alphabetArray.count; i++)
{
if (section==i)
{
headerView.text= [alphabetArray objectAtIndex:i];
headerView.textColor = [UIColor blackColor];
headerView.font = [UIFont fontWithName:#"Montserrat-Medium" size:12.0];
}
}
return headerView;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (searchEnabled)
{
return nil;
}
else
{
NSString *title;
for (int i=0; i<alphabetArray.count; i++)
{
if (section==i)
{
title= [alphabetArray objectAtIndex:i];
}
}
return title;
}
}
-(NSMutableArray *)getArrayOfRowsForSection:(NSInteger)section
{
NSString *rowTitle;
NSString *sectionTitle;
NSMutableArray *rowContainer=[[NSMutableArray alloc]initWithCapacity:0];
for (int i=0; i<alphabetArray.count; i++)
{
if (section==i) // check for right section
{
sectionTitle= [alphabetArray objectAtIndex:i]; //getting section title
for (NSString *title in tableDataArray)
{
rowTitle=[title substringToIndex:1]; //modifying the statement to its first alphabet
if ([rowTitle isEqualToString:sectionTitle]) //checking if modified statement is same as section title
{
[rowContainer addObject:title]; //adding the row contents of a particular section in array
}
}
}
}
return rowContainer;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (searchEnabled)
{
return [self.searchResult count];
}
else
{
NSMutableArray* rowArray=[[NSMutableArray alloc]init];
rowArray=[self getArrayOfRowsForSection:section];
return rowArray.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
if (searchEnabled)
{
cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row];
}
else
{
NSMutableArray* rowArray=[[NSMutableArray alloc]init];
rowArray=[self getArrayOfRowsForSection:indexPath.section];
NSString *titleToBeDisplayed=[rowArray objectAtIndex:indexPath.row];
cell.textLabel.text = titleToBeDisplayed;
}
cell.textLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.7];
return cell;
}
Its hard to get the reason from the code(Seems like it is ok).Please check what is ur UITableViewStyle in the storyboard.If it is plain then try to make it :
UITableViewStyleGrouped
Try this
In
- (void)viewDidLoad {
self.tblView.backgroundColor=[UIColor clearColor];
}
and in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor=[UIColor clearColor];
}

TableView not reloading properly

I am making an settings screen in which you can select stations via a uisearchbar. I have a sectioned tableview, with the first letter of a station as the header and every station is categorized by it's first letter. So far so good.
I habe 2 NSMutableArray's with, per section, the stations. One is the unfiltered array (Which I use when I don't have it filtered) and the other one, when I am searching for something. (I do this via a predicate). On every keypress on the keyboard I do a [self.tableView reloadData]; this works, HOWEVER the scrollview stays too long! So you can scroll way past how many results are actually in the selected array. This causes a crash, because it's trying to get objects that don't exist.
So it seems like the tableview isn't counting the array right or something?
Is anyone familiar with this problem?
Here is some code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (self.searching) {
return [self.tableFilterd count];
} else {
return [self.tableData count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"Rows for section");
// Return the number of rows in the section.
if (self.searching) {
NSLog(#"Editing section: %i, count %i", section, [[self.tableFilterd objectAtIndex:section] count]);
return [[self.tableFilterd objectAtIndex:section] count];
} else {
NSLog(#"Not editing");
return [[self.tableData objectAtIndex:section] count];
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
SettingsHeaderCell *cell = [[[SettingsHeaderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"HeaderCell"] autorelease];
cell.labelLetter.text = [[self.tableLetters objectAtIndex:section] capitalizedString];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 52;
}
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
SettingsCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[SettingsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (self.searching) {
StationObject *object = (StationObject *)[[self.tableFilterd objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
[cell setStationObject:object];
} else {
StationObject *object = (StationObject *)[[self.tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
[cell setStationObject:object];
}
return cell;
}
You might have solved this by now but I suspect you aren't emptying either arrays. In the method:
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText
{
//Remove all objects first
[self.tableFiltered removeAllObjects];
[self.tableData removeAllObjects];
Also you only need to call [self.tableView reloadData]; in textDidChange, not in the other three methods. Hope this helps.

How to keep selection of untouched cells intact?

I have a tableView where I select cells and add the data in an array, I also have the option of swiping and then deleting a particular cell which eventually deletes the data from the array.
The problem is that once I delete a row, I lose all my selection state after I reload the table,
For that I checked again with the selection array and reselected all these cells,
BUT I am stuck at one place, Much before I actually delete a cell and reload the tableView, as soon as I swipe over a cell, selection state of all other cells also go away.
NOTE: I have two arrays, one with list of itmes to be displayed in the tableView and one with the selected items.
Here is some code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.contactList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleGray;
[cell.textLabel setTextColor:[UIColor colorWithRed:103.0/255.0 green:103.0/255.0 blue:103.0/255.0 alpha:1.0]];
[cell.textLabel setFont:[UIFont fontWithName:#"ITCAvantGardeStd-Bk" size:14.0]];
if (![[[self.contactList objectAtIndex:indexPath.row] objectForKey:#"nickName"] isEqualToString:#""])
cell.textLabel.text = [NSString stringWithFormat:#"%#",[[self.contactList objectAtIndex:indexPath.row] objectForKey:#"nickName"]];
else
cell.textLabel.text = [NSString stringWithFormat:#"%# %#",[[self.contactList objectAtIndex:indexPath.row] objectForKey:#"firstName"],[[self.contactList objectAtIndex:indexPath.row] objectForKey:#"lastName"]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Selected cell index==>%d\n",indexPath.row);
//NSString *emailID = [NSString stringWithFormat:#"%#",[[self.contactList objectAtIndex:indexPath.row] objectForKey:#"email_key"]];
NSLog(#"emailID==>%#\n",[self.contactList objectAtIndex:indexPath.row]);
[self.emailShareList addObject:[self.contactList objectAtIndex:indexPath.row]];
//[self.emailShareList insertObject:emailID atIndex:indexPath.row];
NSLog(#"Array value==>%#\n",self.emailShareList);
//[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"deSelected cell index==>%d\n",indexPath.row);
NSString *emailID = [NSString stringWithFormat:#"%#",[[self.contactList objectAtIndex:indexPath.row] objectForKey:#"email_key"]];
NSLog(#"emailID==>%#\n",emailID);
[self.emailShareList removeObject:[self.contactList objectAtIndex:indexPath.row]];
NSLog(#"deSelect row Array value==>%#\n",self.emailShareList);
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
if(indexPath.row != 0)
{
NSString *contactID = [[self.contactList objectAtIndex:indexPath.row] objectForKey:#"contactId"];
NSLog(#"content on delete row==>%#\n",contactID);
[self.contactList removeObjectAtIndex:indexPath.row];
[self deleteContactToServer:contactID];
}
}
[contactTableView reloadData];
for (int i = 0; i < [self.emailShareList count]; i++)
{
for (int j = 0; j < [self.contactList count]; j++)
{
if([[[self.contactList objectAtIndex:j] valueForKey:#"email"] isEqualToString: [[self.emailShareList objectAtIndex:i] valueForKey:#"email"]])
{
NSIndexPath *path1 = [NSIndexPath indexPathForRow:j inSection:0];
[contactTableView selectRowAtIndexPath:path1 animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCellEditingStyle style = UITableViewCellEditingStyleNone;
if(indexPath.row != 0)
style = UITableViewCellEditingStyleDelete;
return style;
}
When you delete an item, you don't necessary have to reload the entire tableview. You could use the – deleteRowsAtIndexPaths:withRowAnimation: method to just remove the cell in question (along with an according model update). This will probably retain your selection.
To keep your selections when entering editing mode (swipe for delete is a special case of editing mode as well) you nee to do two things:
First, enable allowsSelectionDuringEditing on your tableView:
self.tableView.allowsSelectionDuringEditing = YES;
Second, create a UITableView subclass and override setEditing:animated: like this:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
NSArray *indexPaths = self.indexPathsForSelectedRows;
[super setEditing:editing animated:animated];
for (NSIndexPath *ip in indexPaths) {
[self selectRowAtIndexPath:ip animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
Personally, I would rather use some sort of custom selection mechanism, when selections are important from a model point of view. I would create a custom cell subclass, add a selection property to it let it change the cell styling accordingly. The build-in features that affect regular table view selections won't cause problems with such an approach.
Here is an additional method of preserving table selections in and out of edit mode without having to subclass UITableView. Add the following to your UITableViewControllerView.
Within viewDidLoad add:
self.tableView.allowsSelectionDuringEditing = YES;
Then override setEditing:animated:
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
NSArray *selectedIndexPaths = [self.tableView indexPathsForSelectedRows];
[super setEditing:editing animated:animate];
for (NSIndexPath *selectedIndexPath in selectedIndexPaths) {
[self.tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}

Unable to insert Cell to UITableView even updating DataSource before using insertRowsAtIndexPaths method

I'm trying to add a new cell below the tapped cell on the event tableView:didSelectRowAtIndexPath
These are my numberOfSectionsInTableView and numberOfRowsInSection methods. The property self.dataTable is the datasource of my TableView.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//i have that - 1 there because i don't want to use the first row of my dataTable.
//On my cellForRowAtIndex method I'm adding a +1 to the indexPath.row to get the right row information
return [self.dataTable count] - 1;
}
My adding method is:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray * newRow = [NSMutableArray arrayWithObjects:#"Columna 1", #"Columna 2", #"Columna 3", #"Columna 4", nil];
//here im adding the new item to the datasource.
[self.dataTable insertObject:newRow atIndex:indexPath.row + 1];
NSArray *newData = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:(indexPath.row + 1) inSection:1], nil];
//self because this clas inherits from UITableView and implements its own delegate and datasource.
//The error is also produced using tableView instead of self.
[self beginUpdates];
[self insertRowsAtIndexPaths:newData withRowAnimation:UITableViewRowAnimationBottom];
[self endUpdates];
}
When the debbugger reach [self endUpdates], the methos numberOfRowsInSection is launched and I can see the return is updated with +1, however im getting this error:
Exception name: NSInternalInconsistencyException Reason: Invalid table view update. The application has requested an update to the table view that is inconsistent with the state provided by the data source.
Edit: Adding cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = #"SWTableCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
UILabel *lblTemp; //Used for creates labels for cell
CGRect readjust, rectTemp;
rectTemp = CGRectMake(0, 0, self.frame.size.width, 15); //Size of the cell
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.frame = rectTemp;
int x = 10; //initial x position for each label
for(int i = 0; i < self.columnCount; i++)
{
rectTemp = CGRectMake(x, 10, COLUMN_WIDTH, 20); //Size of each column
lblTemp = [[UILabel alloc] initWithFrame:rectTemp]; //inicialize label
lblTemp.tag = i;
[lblTemp setUserInteractionEnabled:NO];
lblTemp.text = (NSString *)[[dataTable objectAtIndex:indexPath.row + 1] objectAtIndex:i];
lblTemp.autoresizingMask= UIViewAutoresizingFlexibleRightMargin;
[lblTemp setHighlightedTextColor:[UIColor whiteColor]];
if(i!=0) lblTemp.textAlignment = UITextAlignmentRight;
else lblTemp.textAlignment = UITextAlignmentLeft;
lblTemp.font = [UIFont systemFontOfSize:10];
[cell setUserInteractionEnabled:YES];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[lblTemp setNumberOfLines:1];
[lblTemp sizeToFit];
readjust = lblTemp.frame;
readjust = CGRectMake(readjust.origin.x, readjust.origin.y, COLUMN_WIDTH, readjust.size.height);
lblTemp.frame = readjust;
[cell.contentView addSubview:lblTemp];
[lblTemp release];
x = x + COLUMN_WIDTH;
}
}
return cell;
}
What else can be causing this issue?
I think
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dataTable count] - 1;
}
should be
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dataTable count];
}
the found or length of an collection is the number of items it is containing. only indexing start with 0. So if you have put 5 items in an array, it has count 5, while the last item's index is 4.
Now I have been blind as well:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//…
[self beginUpdates];
[self insertRowsAtIndexPaths:newData withRowAnimation:UITableViewRowAnimationBottom];
[self endUpdates];
}
must be
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//…
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:newData withRowAnimation:UITableViewRowAnimationBottom];
[tableView endUpdates];
}

UITableView don't react on a tap

I have a fully working grouped tableview. But when i just tap on a cell - nothing happens. willSelectRowAtIndexPath is not even being called. All outlets are set correctly, userInteractions are enabled, tableView is the only view on a page. Googling for hours made no success. Everyone has a selection feature from the begining and someone even tries to ged rid of it. Help, please.
Code (bad one, i know):
//
// VKFriendsPage.m
// vKontakte
//
// Created by ASPCartman on 3/29/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "VKFriendsPage.h"
#import "UIImage+Resize.h"
#import "VKProfilePage.h"
#implementation VKFriendsPage
#synthesize friends,onlineFriends,VKId,connection,avatarCache;
-(void) didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
NSLog(#" Memary Wornenk!!! OLOLOLOLOL!");
}
-(void)viewWillAppear:(BOOL)animated
{
table.backgroundColor=[UIColor clearColor];
if ([connection.myVkontakteID isEqual:VKId]) {
friends=[connection.myVKProfile friendsAll];
onlineFriends=[connection.myVKProfile friendsOnline];
}
self.avatarCache = [NSMutableDictionary dictionaryWithCapacity:2];
queue = [NSOperationQueue new];
table.allowsSelection=YES;
}
#pragma mark Table Data Source Protocol Implementation
//Количество строк в секции
//0ая - Ребята онлайн
//1ая - Все
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
if (section==0) {
return [onlineFriends count];
}else if (section==1) {
return [friends count];
}else {
return 0;
}
}
//Создание клеток
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Инит клетки
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"FriendCell"];
//Выбор секции
NSArray *currentMan;
if (indexPath.section==0) {
currentMan=[onlineFriends objectAtIndex:indexPath.row];
}else if (indexPath.section==1)
{
currentMan=[friends objectAtIndex:indexPath.row];
}else {
currentMan=nil;
}
cell.selectionStyle=UITableViewCellSelectionStyleBlue;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=[currentMan objectAtIndex:1];
cell.detailTextLabel.text=#"TODO: Статусы...";
if ([avatarCache objectForKey:[currentMan objectAtIndex:2]]==nil) {
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:indexPath,#"Path",[currentMan objectAtIndex:2],#"URLString",nil];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:#selector(fetchAvatar:)
object:dic];
[cell.imageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"profileIco" ofType:#"jpg"]]];
[queue addOperation:operation];
[operation autorelease];
}else {
[cell.imageView setImage:[avatarCache objectForKey:[currentMan objectAtIndex:2]]];
}
return [cell autorelease];
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
VKProfilePage *profilePage=[[VKProfilePage alloc] init];
profilePage.title=[tableView cellForRowAtIndexPath:indexPath].textLabel.text;
profilePage.connection=connection;
NSString *VKID;
if (indexPath.section==0) {
VKID=[[onlineFriends objectAtIndex:indexPath.row] objectAtIndex:0];
}else if (indexPath.section==1) {
VKID=[[friends objectAtIndex:indexPath.row] objectAtIndex:0];
}
profilePage.profile=[connection getProfileOf:VKID];
[self.navigationController pushViewController:profilePage animated:YES];
[profilePage release];
}
-(void)fetchAvatar:(NSDictionary *)dic
{
NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:[dic objectForKey:#"URLString"]]];
UIImage *image=[UIImage imageWithData:imageData];
UIImage *resizedImage = [image thumbnailImage:70 transparentBorder:0 cornerRadius:5 interpolationQuality:kCGInterpolationHigh];
[avatarCache setObject:resizedImage forKey:[dic objectForKey:#"URLString"]];
[self performSelectorOnMainThread:#selector(updateRow:) withObject:[dic objectForKey:#"Path"] waitUntilDone:NO];
}
-(void)updateRow:(NSIndexPath *) ip
{
[table reloadRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationNone];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
{
return NO;
};
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section==0) {
return #"Online";
}else {
return #"Offline";
}
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return nil;
};
#end
Did you implement something like this in your UITableView delegate?
- (BOOL)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
Sorry, wrong assumption and a compiler warning ignored...
This is not the solution.
I did some experiments and found the property isUserInteractionEnabled on the UITableView View ancestor. If set to NO, the cell selection is disabled (as is any other interaction). In this case it is impossible to move the views cells up and down.