Populating cell in UITableView with unique items - iphone

I have an array of URL Links that I'm trying to load into a Grouped UITableView. The goal being that I have numberOfSectionsInTableView return [url count] so I can have number of sections equal to the number of url links. Then in numberOfRowsInSection returns 1 so I only populate 1 URL for each section.
The trouble I'm having is to ensure that cellForRowAtIndexPath won't keep grabbing the first url link. I suspect it's because it's always grabbing the first url because the rowIndex is always zero.
Any ideas how to ensure that each cell in my UITableView is populated with a different url link?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [newsItems count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (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 = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 5;
}
// Configure the cell. Get the title of the news item that was parsed out
int newsItemIndex = [indexPath indexAtPosition: [indexPath length] - 1];
[cell.textLabel setText:[[newsItems objectAtIndex: newsItemIndex] objectForKey: #"link"]];
return cell;
}

From what i see here you are making many sections with 1 row each...im not sure that ayou are getting the index correctly you can do something like this
int newsIntemIndex=indexPath.section
that should do it for you

Related

Table does not enter in number of sections and in number of rows

I am using tableView but it does not enter in number of sections and number of rows in tableView here is the code
here is the code for number of sections and number of rows.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
Number of Rows in Sections Code
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"Cell is");
return 1;
}
Cell Values
- (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];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
MultipleDetailViewsWithNavigatorAppDelegate *appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
cell.textLabel.font=[UIFont fontWithName:#"Helvetica" size:16];
cell.textLabel.textAlignment=UITextAlignmentLeft;
PublishData *theCellData = [appDelegate.publishArray objectAtIndex:indexPath.row];
NSLog(#"Cell is");
cell.textLabel.text =#"Test";
return cell;
}
Have you set datasource and delegate for the table view? I suspect not. Please set it in code or xib and check.
How you are have added table view ?. if you have added using interface builder then set your datasoure for the tableview. If you have added pro-grammatically then set [tableview setDatasoucre:self];

EXE Bad Access when Scrolling UITableView - iPhone

I have a simple UITableView with 9 cells. When I move the table up or down by scrolling, I get EXE bad access. NSZombieMode points at the cellForRowAtIndexMethod.
- (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];
}
cell.textLabel.text = [lineArray objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Can anyone suggest what is wrong ?
If ARC is disabled then add autorelease when you create cell
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
This can be the reason of the leaks. Check also lineArray since it used like ivar and probably this array was released at some point.
My guess is you're trying to access an element in your lineArray that is out of bounds.
IE: indexPath.row is returning a 6 when you only have 3 elements in your lineArray.
It's occurring when you scroll down as it triggers cellForRowAtIndexPath to be called on higher number rows (rows that with indexPath.row > 3 for example)
I'll go one more step and guess that you're probably statically returning numberOfRowsForSection.
Setting it to lineArray.count should fix it.
According to my understanding :-
1) in the lineArray you have some 9 items but in the numberOfRowsInSection you are returning the rowCount more than the items in the array and so it crashes and points to ceelForRowAtIndex.
2) Here is the sample code for your understanding :-
- (void)viewDidLoad
{
[super viewDidLoad];
lineArray = [[NSMutableArray alloc]initWithObjects:#"1",#"2",#"3",#"4",#"5", nil];
tableView1 = [[UITableView alloc]init];
tableView1.delegate = self;
tableView1.dataSource = self;
tableView1 .frame =self.view.frame;
[self.view addSubview:tableView1];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [lineArray count];
//return ;
}
- (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];
}
cell.textLabel.text = [lineArray objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

Load more cells "willDisplayCell" Problem

i have a problem with willDisplayCell it doesn't stop fetch Xml from the url
my code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [itemsToDisplay count] + 1;
}
- (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];
}
if (indexPath.row == [itemsToDisplay count]) {
cell.textLabel.text = #"Load More...";
} else {
MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
cell.textLabel.text = item.title;
}
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == [itemsToDisplay count]) {
// Parse
NSURL *feedURL = [NSURL URLWithString:#"http://gdata.youtube.com/feeds/api/users/RayWilliamJohnson/uploads?v=2&alt=rss&sort_field=added&start-index=21&max-results=20"];
feedParser = [[MWFeedParser alloc] initWithFeedURL:feedURL];
feedParser.delegate = self;
feedParser.feedParseType = ParseTypeFull; // Parse feed info and all items
feedParser.connectionType = ConnectionTypeAsynchronously;
[feedParser parse];
[self.tableView reloadData];
}
}
its look like he is in kind of loop that doesnt stop fetch xml
so how can i fix it so i will only called once?
tnx
The problem is that you have called reloadData within tableView:willDisplayCell:. reloadData will cause every UITableViewCell to get drawn again, means willDisplayCell will get called for every cell that will be visible on screen. Seems to me like you have an infinite loop there.
tableView:willDisplayCell: is definitely the wrong place to be doing that XML parsing. willDisplayCell should just be used to prepare the UI. I can't say for certain, but perhaps a better place to the XML parsing would be in your view controller's init method.

Table-view rows

Hey guys when I show talbeview there are a lot of empty rows... How can I make the number of rows shown exactly equal to [NSARRAY COUNT]
I am sure
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return the right number of rows that it should show
Set the table footerView to a view that doesn't render anything (but not nil).
tableView.tableFooterView = [[[UIView allocate] init] autorelease];
You've got the right place and really have the answer already. Here it is in code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [yourDataArrayName count];
}
Substitute the actual name of your table for tableView and the actual name of your array for yourDataArrayName, obviously.
You've got the right place and really have the answer already. Here it is in code:
try it may be helped....
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return totalRows;
}
totalRows= (you want so no. of rows )
and
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"%i %i",indexPath.row,indexPath.section];
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.highlighted = YES
}

CoreDataBooks Add new cell to tableview

I recently downloaded Apple's sample app CoreDataBooks and for the sake of learning decided to add a cell to the last record in the RootViewController.m table view, which would display the number of records that were fetched.
The code I added is shown below. I didn't get to the point of adding the fetch count because I am getting bad results from my code. If you download CoreDataBooks and replace the table view datasource methods with the code here you will see the following:
the new cell is added to the bottom of the table view.
when you scroll back to the top of the table another cell receives the formatting that is only supposed to be given to the last cell of the table. In my case, under the author section of 'Bill Bryson' the book title 'Notes From a Big Country' turns blue and is centered when you scroll back to the top of the table.
Any help in resolving this would be appreciated.
Here is the link for the sample app CoreDataBooks: link text
Here is the modified code for RootViewController.m:
/*
The data source methods are handled primarily by the fetch results controller
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[fetchedResultsController sections] count] + 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section < [[fetchedResultsController sections] count])
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
return 1;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSLog(#"indexPath.section: %i",indexPath.section);
if (indexPath.section < [[fetchedResultsController sections] count]){
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
}
else{
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = (#"This is a NEW Cell...");
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.textLabel.textColor = [UIColor blueColor];
}
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
// Configure the cell to show the book's title
Book *book = [fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = book.title;
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {
// Display the authors' names as section headings.
if (section < [[fetchedResultsController sections] count])
return [[[fetchedResultsController sections] objectAtIndex:section] name];
return #"";
}
That's all. Thank-you for your time.
I see two solutions:
Use a different cell identifier for your cell.
To do this, define a new cell identifier after the other and move the cell reuse code inside the two different cell cases:
static NSString *CellIdentifier = #"Cell";
static NSString* myCellIdentifier = #"MyCell"; // new cell identifier
// move inside two cell cases
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSLog(#"indexPath.section: %i",indexPath.section);
if (indexPath.section < [[fetchedResultsController sections] count]){
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // only reuse this type of cell here
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
}
else{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCellIdentifier]; // only reuse this type of cell here
if (cell == nil) {
2 . Override all of the applicable formatting when configuring the cells:
cell.textLabel.text = (#"This is a NEW Cell..."); // You are already overriding the only cell attribute that the book cell configures
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.textLabel.textColor = [UIColor blueColor];
}
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
// Configure the cell to show the book's title
Book *book = [fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = book.title;
cell.textLabel.textAlignment = UITextAlignmentLeft; // I assume Left is the default alignment
cell.textLabel.textColor = [UIColor blackColor]; // I assume black is the default color
}