Read/Unread cell (using drawrect) - iphone

My app downloading posts from internet to UITableViewCell. When user taps on cell, i am need to set this article as read (like on Mail.app). I dont know how to do that.
I am adding url (when user press cell) to database (like "url|url|url"), open detail view. And when he goes back to UITableView checking existing url in DB. Table now is too slow!
Can u help me and say what other method i can use to do that? Like in mail.app. I am using custom cell and code in drawRect method. Please, help
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"%d",indexPath.row];
NSMutableArray *array=[NSMutableArray array];
if([[NSUserDefaults standardUserDefaults] objectForKey:#"readPostS"]!=nil)
[array addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"readPostS"]];
NSUInteger indexOfObject = [array count]>0?[array indexOfObject:[NSNumber numberWithInt:indexPath.row]]:100000; //I add this, because row don't want update using updateRowAtIndexPath
QuickCell *cell = (QuickCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil || (indexOfObject!=NSNotFound && indexOfObject!=100000))
{
cell = [[[QuickCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.frame = CGRectMake(0.0, 0.0, 320.0, 68.0);
cell.accessoryType = UITableViewCellAccessoryNone;
if([self.subject count]>0)
{
[self.allImages addObject:[NSNull null]];
NSString *userNameLJ = [NSString stringWithFormat:#"%#",[self.journalurl objectAtIndex:indexPath.row]];
userNameLJ = [userNameLJ stringByReplacingOccurrencesOfString:#"http://"
withString:#""];
userNameLJ = [userNameLJ stringByReplacingOccurrencesOfString:#".livejournal.com"
withString:#""];
userNameLJ = [userNameLJ stringByReplacingOccurrencesOfString:#"community/"
withString:#""];
NSString *postURL = [NSString stringWithFormat:#"http://m.livejournal.com/read/user/%#/%#",userNameLJ,[self.ditemid objectAtIndex:indexPath.row]];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[self.subject objectAtIndex:indexPath.row] forKey:#"title"];
[dict setObject:[Utilities replaceForCell:[Utilities flattenHTML:[self.entry objectAtIndex:indexPath.row]]] forKey:#"description"];
if([[database executeQuery:#"SELECT * FROM read WHERE posts=?;",postURL] count]>0)
{
//if this post is read
[dict setObject:[NSNumber numberWithBool:NO] forKey:#"highlighted"];
}
else {
[dict setObject:[NSNumber numberWithBool:YES] forKey:#"highlighted"];
}
[dict setObject:[self.journalname objectAtIndex:indexPath.row] forKey:#"nick"];
[cell setContentForCell:dict];
BOOL trueOrFalse = [[self.allImages objectAtIndex:indexPath.row] isKindOfClass:[NSNull class]]?YES:NO;
if(trueOrFalse)
{
if (tableView.dragging == NO && tableView.decelerating == NO)
{
//if no image cached now, download it
if(indexPath.row<6 && self.updating==YES)
[self startDownloading:indexPath];
if(self.updating==NO)
[self startDownloading:indexPath];
}
}
else
{
[cell setImageForCell:[self.allImages objectAtIndex:indexPath.row]];
}
}
if(indexOfObject!=NSNotFound && indexOfObject!=100000)
{
//delete this row from userdefaults
[array removeObjectAtIndex:indexOfObject];
if([array count]>0)
[[NSUserDefaults standardUserDefaults] setObject:array forKey:#"readPostS"];
else [[NSUserDefaults standardUserDefaults] removeObjectForKey:#"readPostS"];
}
}
return cell;
}
//////////////////////////////////////////////////////////////
///////-(void)viewWillApear
//////////////////////////////////////////////////////////////
NSMutableArray *readPosts = [NSMutableArray array];
if([[NSUserDefaults standardUserDefaults] objectForKey:#"readPostS"]!=nil)
{
[readPosts addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"readPostS"]];
self.read = (NSMutableArray*)[database executeQuery:#"SELECT * FROM read;"];
for(int i=0;i<[readPosts count];i++)
{
[self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[[readPosts objectAtIndex:i] intValue] inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}
}
///////////////////////////////////////////////
///////THIS FILE OPENS POSTS FOR READING
///////////////////////////////////////////////
if([[database executeQuery:#"SELECT * FROM read WHERE posts=?;",self.postURL] count]==0)
{
[database executeNonQuery:#"INSERT INTO read VALUES (?);",self.postURL];
NSMutableArray *defaults = [NSMutableArray array];
if([[NSUserDefaults standardUserDefaults] objectForKey:#"readPostS"]!=nil)
defaults = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"readPostS"]];
[defaults addObject:[NSNumber numberWithInt:self.currentIndex]];
[[NSUserDefaults standardUserDefaults] setObject:defaults forKey:#"readPostS"];
}
Ok. I think, my code is to bad and u can understand, what i am doing here) Answer on other question. How i can update hidden cells? User see only 6 cell in a moment, i need to update, for example, 10 cells. How?
Or, how reload cell, if it allready exists? Lets say -
NSString *CellIdentifier = [NSString stringWithFormat:#"%d",indexPath.row];
QuickCell *cell = (QuickCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[QuickCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.frame = CGRectMake(0.0, 0.0, 320.0, 68.0);
cell.accessoryType = UITableViewCellAccessoryNone;
.........}
now cell exists and if i call reload data nothing happen. And reloadCellAtIndexPath does not working too, because cell has unique identifier and ixists.
HOW I CAN RELOAD CELL AND CALL DRAWRECT AGAIN?))

You can implement this functionality by adding your code in the
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//Add the indexPath.row values in an array.
}
Add the custom functionality (like showing Title in BOLD for unread) in the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Check if the indexPath.row is available in the array
//If yes then Normalize the fonts
//Else Keep them BOLD.
}

Unless you're really doing some drawing, don't implement in drawRect:. I don't know you're specific code, so that may not be correct in your case.
To reload the entire table (all cells), look at UITableView's - (void)reloadData
To reload a specific cell, look at UITableView's - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation and use it to reload the cell in question.

Related

Searching a UITableview

I am trying to do a search in a UITableview. I have implemented the UISearchDisplayDelegate, UISearchBarDelegate method at the correct way. This is how my cellForRowAtIndexPath looks like.
- (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 (tableView == self.searchDisplayController.searchResultsTableView){
Contact *contact = [self.filteredListContent objectAtIndex:indexPath.row];
NSString *text = [NSString stringWithFormat:#"%# %#",contact.name,contact.firstName];
NSLog(#"CellForRowAtIndexPath contact text is %#",text);
cell.textLabel.text = text;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}else{
NSString *alphabet = [firstIndex objectAtIndex:[indexPath section]];
//---get all states beginning with the letter---
NSPredicate *predicate =
[NSPredicate predicateWithFormat:#"SELF.name beginswith[c] %#",alphabet];
NSArray *contacts = [listContent filteredArrayUsingPredicate:predicate];
Contact *contact = [contacts objectAtIndex:indexPath.row];
NSString *text = [NSString stringWithFormat:#"%# %#",contact.name,contact.firstName];
cell.textLabel.text = text;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
return cell;
}
And this is my filterContentForSearchText method
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
for (Contact *contact in listContent)
{
NSString *searchString = [NSString stringWithFormat:#"%# %#",contact.name,contact.firstName];
NSRange range = [searchString rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
[self.filteredListContent addObject:contact];
[self.searchDisplayController.searchResultsTableView reloadData];
}
}
}
The strange thing is. In my cellForRowAtIndexPath it returns me the correct data. But the tableview itselfs keeps given me the NO RESULTS label.
Any help with this?
Follow this tutorial how-to-add-search-bar-uitableview ..
Just see the method cellForRowAtIndexPath: in which how to set the search array when user searched record from UISearchBar...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"RecipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
}
return cell;
}
Use an array to show data in table(showDataArray)
Use an array to store all input values(dataArray)
Use showDataArray to populate your table always
In the searchfield when charecter range changes call a method to filter the data using predicate from dataArray
Save the value to showDataArray
Call for table reloadData
First add UISearchBar on top of UITabelView documentation
And then Take Two NSMutableArray and add one array to another array in ViewDidLoad method such like,
self.listOfTemArray = [[NSMutableArray alloc] init]; // array no - 1
self.ItemOfMainArray = [[NSMutableArray alloc] initWithObjects:#"YorArrayList", nil]; // array no - 2
[self.listOfTemArray addObjectsFromArray:self.ItemOfMainArray]; // add 2array to 1 array
And Write following delegate Method of UISearchBar
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText
{
NSString *name = #"";
NSString *firstLetter = #"";
if (self.listOfTemArray.count > 0)
[self.listOfTemArray removeAllObjects];
if ([searchText length] > 0)
{
for (int i = 0; i < [self.ItemOfMainArray count] ; i = i+1)
{
name = [self.ItemOfMainArray objectAtIndex:i];
if (name.length >= searchText.length)
{
firstLetter = [name substringWithRange:NSMakeRange(0, [searchText length])];
//NSLog(#"%#",firstLetter);
if( [firstLetter caseInsensitiveCompare:searchText] == NSOrderedSame )
{
// strings are equal except for possibly case
[self.listOfTemArray addObject: [self.ItemOfMainArray objectAtIndex:i]];
NSLog(#"=========> %#",self.listOfTemArray);
}
}
}
}
else
{
[self.listOfTemArray addObjectsFromArray:self.ItemOfMainArray ];
}
[self.tblView reloadData];
}
And Write in cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Foobar"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Foobar"];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.textColor = [UIColor blackColor];
}
cell.textLabel.text = [self.listOfTemArray objectAtIndex: indexPath.row];
return cell;
}

NSRangeException Error with NSArray - While setting image view in another ViewController

I'm getting an image from the AppDelegate, then setting it to the ViewController's table.imageView property. It's throwing me an NSRangeException:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (2) beyond bounds (2)'
I made sure my array and rows are counted by [array count]. Very confused. Here's the code:
#pragma mark - viewWillAppear
- (void)viewWillAppear:(BOOL)animated {
PBAppDelegate *dataObject = (PBAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *titleRead = dataObject.title;
NSString *descRead = dataObject.desc;
UIImage *imageRead = dataObject.image;
if ([titleRead isEqualToString:#"" ] || [descRead isEqualToString:#""]) {
// it's nil
} else {
if (titleRead) {
[data addObject:[NSArray arrayWithObjects:titleRead, descRead, imageRead, nil]];
dataObject.title = #"";
dataObject.desc = #"";
[tableView reloadData];
}
NSUserDefaults *dataDefaults = [NSUserDefaults standardUserDefaults];
[dataDefaults setObject:[NSArray arrayWithArray:data] forKey:#"dataArrayKey"];
[dataDefaults synchronize];
}
}
#pragma mark - viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
data = [[NSMutableArray alloc] init];
self.data = [[NSUserDefaults standardUserDefaults] objectForKey:#"dataArrayKey"];
[tableView reloadData];
}
#pragma mark - Table Datasource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [[data objectAtIndex:indexPath.row] objectAtIndex:0];
cell.detailTextLabel.text = [[data objectAtIndex:indexPath.row] objectAtIndex:1];
cell.imageView.image = [UIImage imageNamed:[[data objectAtIndex:indexPath.row] objectAtIndex:2]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
It's probably this line:
cell.imageView.image = [UIImage imageNamed:[[data objectAtIndex:indexPath.row] objectAtIndex:2]];
The console output says there are 2 elements in the array. Elements start at 0; so you can access objectAtIndex:0 and objectAtIndex:1. Two would be the 3rd element of the array, and is out of bounds.
Sorry if that's all obvious, just taking a quick stab... Enjoy. :)
EDIT
Actually, the issue could be that imageRead is nil when you add it to the array. That would cause the array to have only 2 elements in it. You may check for that, and/or use [NSNull null] if you don't have an image...
Make sure the array actually has 3 elements by doing something like this:
NSArray *array = [data objectAtIndex:indexPath.row];
if ([array count] > 2)
{
cell.imageView.image = [UIImage imageNamed:[array objectAtIndex:2]];
}

search bar not working?

i have a SQL file where 5 different type of data is stored. I am adding this data in a dictionary specified with keys. and then i am adding this dictionary to tableData array as a dataSource for table and searchBar. But it is not searching anything.
adding code below
- (void)viewDidLoad {
[super viewDidLoad];
dataSource =[[NSMutableArray alloc] init];
tableData = [[NSMutableArray alloc]init];
searchedData = [[NSMutableArray alloc]init];
NSString *query = [NSString stringWithFormat:#"SELECT * FROM Vegetables"];
SQLite *sqlObj1 = [[SQLite alloc] initWithSQLFile:#"ShoppersWorld.sqlite"];
[sqlObj1 openDb];
[sqlObj1 readDb:query];
// [query release];
for (int i=0; i<[dataSource count]; i++) {
NSLog(#"data:%#",[dataSource objectAtIndex:i]);
}
while ([sqlObj1 hasNextRow])
{
NSString *name=[sqlObj1 getColumn:1 type:#"text"];
NSString *price=[sqlObj1 getColumn:2 type:#"text"];
NSString *quantity=[sqlObj1 getColumn:3 type:#"text"];
NSString *unit=[sqlObj1 getColumn:4 type:#"text"];
NSString *total=[sqlObj1 getColumn:5 type:#"text"];
dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys: name,#"nameOfVegetables",
price,#"priceOfVegetables",
quantity,#"quantityOfVegetables",
unit,#"unitOfVegetables",
total,#"totalPriceOfVegetables",nil];
//NSLog(#"results:%# %#",dict);
[dataSource addObject:dict];
}
[tableData addObjectsFromArray:dataSource];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
// Configure the cell.
else {
cell.productLbl.text= [NSString stringWithFormat:#"%#",[[tableData objectAtIndex:indexPath.row]objectForKey:#"nameOfVegetables"] ];
cell.bPriceLbl.text = [NSString stringWithFormat:#"Rs %d/Kg",
[[[tableData objectAtIndex:indexPath.row] objectForKey:#"priceOfVegetables"] intValue]];
cell.qtyLbl.text = [NSString stringWithFormat:#"QTY: %# %#",[[tableData objectAtIndex:indexPath.row]
objectForKey:#"quantityOfVegetables"],[[tableData objectAtIndex:indexPath.row] objectForKey:#"unitOfVegetables"]] ;
cell.tPriceLbl.text = [NSString stringWithFormat:#"TOTAL: %#",[[tableData objectAtIndex:indexPath.row]
objectForKey:#"totalPriceOfVegetables"]];
}
return cell;
}
#pragma search operations
- (IBAction)search:(id)sender{
sBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,40,320,30)];
sBar.delegate = self;
[self.view addSubview:sBar];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
// only show the status bar’s cancel button while in edit mode
[sBar setShowsCancelButton:YES animated:YES];
sBar.autocorrectionType = UITextAutocorrectionTypeNo;
// flush the previous search content
[tableData removeAllObjects];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
[sBar setShowsCancelButton:NO animated:YES];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
[tableData removeAllObjects];// remove all data that belongs to previous search
if([searchText isEqualToString:#""] || searchText==nil){
[tableview reloadData];
return;
}
NSInteger counter = 0;
for(NSString *name in dataSource)
for (int i = 0; i < [dataSource count]; i++)
{
NSMutableDictionary *temp = (NSMutableDictionary*) [dataSource objectAtIndex:i];
NSString *name = [NSString stringWithFormat:#"%#", [temp valueForKey:#"nameOfVegetables"]];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(r.location != NSNotFound)
{
if(r.location== 0)//that is we are checking only the start of the names.
{
[tableData addObject:name];
}
}
counter++;
[pool release];
}
[tableview reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
sBar.hidden= YES;
// if a valid search was entered but the user wanted to cancel, bring back the main list content
[tableData removeAllObjects];
[tableData addObjectsFromArray:dataSource];
#try{
[tableview reloadData];
}
#catch(NSException *e){
}
[sBar resignFirstResponder];
sBar.text = #"";
}
In search delegate methods you manipulate not with searchedData but tableData array. As these name suggest, array searchedData is supposed to store filtered data.
By the way, your approach to use sqlite for data source and absorbing all database into array is wrong. In cellForRowAtIndexPath read from sqlite database only data you need at the moment.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// load from sqlite only data for this cell.
// Use searchBar.text with sqlite's LIKE to filter data from database
NSUInteger row = [indexPath row];
static NSString *CellIdentifier = #"SignsCellIdentifier";
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSString *sql = [NSString stringWithFormat:#"SELECT fieldNameFromTable FROM TableName WHERE FieldToLookForIn LIKE \"%%%#%%\" LIMIT 1 OFFSET %u", searchBar.text ? searchBar.text : #"", row];
sqlite3_stmt *stmt;
int res = sqlite3_prepare_v2(database, [sql UTF8String], -1, &stmt, NULL);
if (res != SQLITE_OK) {
NSLog(#"sqlite3_prepare_v2() failed"];
return nil;
}
if (sqlite3_step(stmt) == SQLITE_ROW) {
const unsigned char *name = sqlite3_column_text(stmt, 0);
cell.text = [NSString stringWithUTF8String:(const char*)name];
}
sqlite3_finalize(stmt);
return cell;
}
How to apply search in this approach? In textDidChange do nothing but call [tableView reloadData]. And in cellForRowAtIndexPath load data with sqlite LIKE using searchBar.text as search term. So reloadData will load only filtered records. searchBarSearchButtonClicked will call only resignFirstResponder of it's caller, removing keyboard off screen. It doesn't need to do anything more because search is already done. searchBarCancelButtonClicked will set text property of it's caller to nil, call reload data and again call resignFirstResponder.
- (void)searchBarCancelButtonClicked:(UISearchBar *)s {
s.text = nil;
[tableView reloadData];
[s resignFirstResponder];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)s {
// search is already done
[s resignFirstResponder];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[tableView reloadData];
}
numberOfRowsInSection should also request db with the same SELECT as in cellForRowAtIndexPath, but with SELECT COUNT. Writing this method will be your homework)

Show data in tableView from NSDictionary in NSMutableArray

I am trying store records in UITableView each cell on reload data from a form. For this when user press add button so so multiple labels should be added to 1st indexPath then on reclicking add button again multiple cells must be added to second cell retaining first.
I am looking to store my data in
MyArray ({
firstindex: 1st record
firstindex: 2nd record
}
{
secondindex : 1st record
secondindex : 2nd record
})
Records are comming from UITextView and saving into table on each Add button click on each row with multiple entries
- (IBAction) Add
{
[myDict setObject:countryTxt.text forKey:#"Country"];
[myDict setObject:cityTxt.text forKey:#"City"];
[myDict setObject:EscortsNumTxt1.text forKey:#"people"];
[myDict setObject:fromDate.text forKey:#"Datetogo"];
[myDict setObject:toDate.text forKey:#"Datetoreach"];
[moreArr addObject:myDict];
NSLog(#"moreDict %#",moreArr);
numberOfrows++;
[moreTable reloadData];
}
I have implemented through this way but showing repeated data on TableView Cell, I am not including frame of UILabel code, as only facing problem in fetchng
- (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] autorelease];
}
countrylbl.text = [[moreArr objectAtIndex:indexPath.row] objectForKey:#"Country"];
citylbl.text = [[moreArr objectAtIndex:indexPath.row] objectForKey:#"City"];
people.text = [[moreArr objectAtIndex:indexPath.row] objectForKey:#"people"];
toDateLbl.text = [[moreArr objectAtIndex:indexPath.row] objectForKey:#"Datetogo"];
fromDteLbl.text = [[moreArr objectAtIndex:indexPath.row] objectForKey:#"Datetoreach"];
}
Try to retrieve the array once like in your methos
- (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] autorelease];
}
NSArray *results = [[NSArray alloc] initWithArray:[moreArr objectAtIndex:indexPath.row]];
NSLog("%#",results); //Check that everything is in the right place
// DO STUFF
[results release];
}
Solution as NSMutableDictionary is overriding the content I dont know why so I allocated it within the function
moreTableDict = [[NSMutableDictionary alloc] init];
[myDict setObject:countryTxt.text forKey:#"Country"];
[myDict setObject:cityTxt.text forKey:#"City"];
[myDict setObject:EscortsNumTxt1.text forKey:#"people"];
[myDict setObject:fromDate.text forKey:#"Datetogo"];
[myDict setObject:toDate.text forKey:#"Datetoreach"];
[moreArr addObject:myDict];
This works for me

I can't fill the cells of my UITableView with a NSArray (Normally it always works, but not in the case)

I have a UITableView with Cells that are filled with the Objects of an NSArray. I´m using the MGTwitterEngine to get Tweets from users. I want to show this tweets in the TableView. Here is the method that is called when the MGTwitterEngine has received all tweets ("namen" and "nachricht" are NSArrays):
- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier
{ NSLog(#"Got statuses for %#:\r%#", connectionIdentifier, statuses);
NSDictionary *userDict = [statuses valueForKey:#"user"];
namen = [[NSArray alloc] initWithObjects: [userDict valueForKey:#"name"], nil];
nachricht = [statuses valueForKey:#"text"];
NSLog(#"%#", namen);
NSLog(#"%#", nachricht);
[table reloadData];
}
Then I did this to fill the cells with the names of the Twitter-users:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [nachricht count];
}
- (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] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [arryData objectAtIndex:indexPath.row];
return cell;
}
Unfortunately, the app crashes whenever the table is reloaded: [table reloadData];. I know that the NSArray got all the right values, because NSLog shows me that. I've tried also to create a other NSArray, like that:
arryData = [[NSArray alloc] initWithObjects:#"iPhone",#"iPod",#"MacBook",#"MacBook Pro",nil];
And I written this into the cellforRowAtIndexPath-methode and and everything worked well. Why does it not work with the "namen"-NSArray?
The NSArray is probably being released - have you examined what's at its memory address?