I have ipad app in which i am using tableView to show data but problem is that it does not show data in tableView here is code for setting cell value
- (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 = UITableViewCellSelectionStyleBlue;
}
if (tableView == tableCategory) {
cell.textLabel.font=[UIFont fontWithName:#"Arial" size:16];
cell.textLabel.textAlignment=UITextAlignmentLeft;
ObjectData*theCellData = [categoryArray objectAtIndex:indexPath.row];
cell.textLabel.text=theCellData.categoryTitle;
return cell;
}
else if (tableView == tableSubCategory)
{
cell.textLabel.font=[UIFont fontWithName:#"Arial" size:16];
cell.textLabel.textAlignment=UITextAlignmentLeft;
ObjectData*theCellData = [subCategoryArray objectAtIndex:indexPath.row];
NSString *test = theCellData.subCategoryTitle;
NSLog(#"Test Cell is %#",test);
cell.textLabel.text = test;
return cell;
}
else if(tableView == tablePublish)
{
cell.textLabel.font = [UIFont fontWithName:#"Arial" size:16];
cell.textLabel.textAlignment = UITextAlignmentLeft;
GetPublishData *theCellData = [publishArray objectAtIndex:indexPath.row];
NSString *test = [NSString stringWithFormat:#"%#:%#:%# in:%#:%#",theCellData.UserName,theCellData.ContentTitle,theCellData.ContentType,theCellData.Catgeory,theCellData.SubCategory];
NSLog(#"Test Cell is %#",test);
cell.textLabel.text = test;
cell.detailTextLabel.text = theCellData.ContenAddedTime;
return cell;
}
else
{
cell.textLabel.font = [UIFont fontWithName:#"Arial" size:16];
cell.textLabel.textAlignment = UITextAlignmentLeft;
GetPublishData *theCellData = [publishArray objectAtIndex:indexPath.row];
NSString *test = theCellData.ContentDescription;
cell.textLabel.text = test;
return cell;
}
}
I have tableView delegate and dataSource but still does not show anything while check count in array it shows items in it but does not show data in tableview
the data which i am showing for else table is not working
What returns your method - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section? Can you show your code?
Also check that you set delegate and datasource for all of tables.
Excuse me for posting this like answer. I haven't reputation for posting in comments.
check for the cell identifier.. have you given any custom cell identifier for the table cells? here you are mentioning it as
NSString *CellIdentifier = #"Cell";
Related
I have an array of names that I'm showing via tableview. You can select up to a total of 3 names, and you cannot re-select the same names. To do this I implemented the following code in cellForRowAtIndexPath:. When I run the code the names come up fine, but there are multiple red cells with names that I did not select.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *sectionTitle = [nameSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionNames = [names objectForKey:sectionTitle];
NSString *name = [sectionNames objectAtIndex:indexPath.row];
cell.textLabel.text = name;
if ([name isEqualToString: self.name1] || [name isEqualToString: self.name2] || [name isEqualToString: self.name3]) {
[cell setUserInteractionEnabled:NO];
cell.backgroundColor = [UIColor redColor];
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
Reading a similar problem here, they were saying that it's because the cells are being reused - but if this were true, how is the tableview still displaying the correct names in the correct position?
I tried to simplify the code into this, and still to no avail, there were multiple red cells.
myIP = [NSIndexPath indexPathForRow:0 inSection:0];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"TableCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *sectionTitle = [nameSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionNames = [names objectForKey:sectionTitle];
NSString *name = [sectionNames objectAtIndex:indexPath.row];
cell.textLabel.text = name;
if (indexPath == myIP) {
cell.backgroundColor = [UIColor redColor];
}
return cell;
}
I can post a screenshot if needed. Note: The intended names were correctly labeled with red.
The issue is happening due to cell re-using. When a cell with red background is re-used it'll still be in red background, you are not re-setting it anywhere in your code. You need to put a else case to your if condition used in cellForRowAtIndexPath: method.
if ([name isEqualToString: self.name1] || [name isEqualToString: self.name2] || [name isEqualToString: self.name3])
{
[cell setUserInteractionEnabled:NO];
cell.backgroundColor = [UIColor redColor];
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
[cell setUserInteractionEnabled:YES];
cell.backgroundColor = [UIColor clearColor];
// Other stuffs
}
I have the following code to populate data on the uitableview cell. For some reason, I see every item exactly two on the tableview.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"takeStockCell";
UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
RackStockTakeStatus *rackStockTakeStatus = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = rackStockTakeStatus.locName;
if ([[rackStockTakeStatus.status lowercaseString] isEqualToString:#"inprogress"])
{
cell.detailTextLabel.textColor = [UIColor blueColor];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%# [%#]",rackStockTakeStatus.status,rackStockTakeStatus.stockTakeByUser];
} else if ([[rackStockTakeStatus.status lowercaseString] isEqualToString:#"completed"])
{
cell.detailTextLabel.textColor = [UIColor colorWithRed:(0/255.0) green:(102/255.0) blue:(0/255.0) alpha:1];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%# [%#]",rackStockTakeStatus.status,rackStockTakeStatus.stockTakeByUser];
} else if ([[rackStockTakeStatus.status lowercaseString] isEqualToString:#"verified"])
{
cell.detailTextLabel.textColor = [UIColor colorWithRed:(0/255.0) green:(102/255.0) blue:(0/255.0) alpha:1];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%# [%#]",rackStockTakeStatus.status,rackStockTakeStatus.stockTakeByUser];
}
}
I am not sure about your method configuring the cell like that. Have you tried to configure it directly into the method cellForRowAtIndexPath ? Not putting the cell as a parameter.
Or just returning the cell in your method configure
cell = [self configureCell...
What I want to do here is set it so that if top3ArrayForSection.count-1 < 1, set the _results bool value of that respective section to NO, and so on. What's happening instead, is that _results is set to NO or YES for the entire table overall, so that I end up with a result like this:
When only the "xperia Z3 compact unlocked" section should say "no items found etc." because it has no cells, the other sections cells shouldn't.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSDictionary *currentSectionDictionary = _matchCenterArray[section];
NSArray *top3ArrayForSection = currentSectionDictionary[#"Top 3"];
if (top3ArrayForSection.count-1 < 1){
_results = NO;
_rowCount = 1;
}
else if(top3ArrayForSection.count-1 >= 1){
_results = YES;
_rowCount = top3ArrayForSection.count-1;
}
return _rowCount;
}
// Cell layout
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Initialize cell
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// No cell seperators = clean design
tableView.separatorColor = [UIColor clearColor];
if (_results == NO) {
// title of the item
cell.textLabel.text = #"No items found, but we'll keep a lookout for you!";
cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
}
else if (_results == YES) {
// title of the item
cell.textLabel.text = _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Title"];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
// price of the item
cell.detailTextLabel.text = [NSString stringWithFormat:#"$%#", _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Price"]];
cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];
// image of the item
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Image URL"]]];
[[cell imageView] setImage:[UIImage imageWithData:imageData]];
}
return cell;
}
Your instance variable _results is one-dimensional. You could replace it with an NSArray and store the values individually, or you could change the logic in your code as such:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSDictionary *currentSectionDictionary = _matchCenterArray[section];
NSArray *top3ArrayForSection = currentSectionDictionary[#"Top 3"];
return (top3ArrayForSection.count-1 < 1) ? 1 : top3ArrayForSection.count-1;
}
// Cell layout
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Initialize cell
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// No cell seperators = clean design
tableView.separatorColor = [UIColor clearColor];
NSDictionary *currentSectionDictionary = _matchCenterArray[indexPath.section];
NSArray *top3ArrayForSection = currentSectionDictionary[#"Top 3"];
if (top3ArrayForSection.count-1 < 1) {
// title of the item
cell.textLabel.text = #"No items found, but we'll keep a lookout for you!";
cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
}
else {
// title of the item
cell.textLabel.text = _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Title"];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
// price of the item
cell.detailTextLabel.text = [NSString stringWithFormat:#"$%#", _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Price"]];
cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];
// image of the item
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Image URL"]]];
[[cell imageView] setImage:[UIImage imageWithData:imageData]];
}
return cell;
}
I have a UiTableView containing Youtube Videos. The Youtube Videos are listed in the TableViewCells with the attributes title, author and link. The problem is when i try to return the value from the selected indexPath it returns another youtube video and not the one selected. why is it not returning the information from the selected video?
cellForRowAtIndexPath;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
video = [items objectAtIndex:indexPath.row];
cell.textLabel.text = video.title;
cell.textLabel.numberOfLines = 2;
UIFont *myFont = [ UIFont fontWithName: #"Arial" size: 12.0 ];
cell.textLabel.font = myFont;
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#\n%# views", video.author, FormatViewCount(video.viewcnt)];
cell.detailTextLabel.numberOfLines = 2;
UIFont *myFont1 = [ UIFont fontWithName: #"Arial" size: 10.0 ];
cell.detailTextLabel.font = myFont1;
[cell.imageView setImageWithURL:[NSURL URLWithString:video.thumb] placeholderImage:[UIImage imageNamed:#"blankvideo"]];
[cell.imageView.layer setBorderWidth:2];
[cell.imageView.layer setBorderColor:[UIColor whiteColor].CGColor];
return cell;
}
didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%#", video.title);
NSLog(#"%#", video.author);
NSLog(#"%#", video.videoid);
NSLog(#"%d", thisObject.selectedRowNow);
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//---------add one more line---------//
video = [items objectAtIndex:indexPath.row];
//---------add one more line---------//
NSLog(#"%#", video.title);
NSLog(#"%#", video.author);
NSLog(#"%#", video.videoid);
NSLog(#"%d", thisObject.selectedRowNow);
}
You need to write:
video = [items objectAtIndex:indexPath.row];
before NSLog.
When you write video = [items objectAtIndex:indexPath.row]; you are just storing the last item that the tableview happens to have requested. No really point in storing it as an attribute of your class.
You can use a similar statement in your didSelectRowAtIndexPath:(NSIndexPath *)indexPath to retrieve the video that is actually selected:
Video *video = [items objectAtIndex:indexPath.row];
I am storing checkbox values (true/false) in database. I want to set checkbox checked/unchecked depending on the values stored in database. how to do that ? my code is,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%i",indexPath.row];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:14.0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:18.0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.frame=CGRectMake(75.0, 50.0, 150.0, 20.0);
cell.textLabel.text=[listArray objectAtIndex:indexPath.row];
NSLog(#"Checked arr size %i",[checkedArr count]);
BOOL checked = [[checkedArr objectAtIndex:indexPath.row] boolValue];
UIImage *image = (checked) ? [UIImage imageNamed:#"white_bg.png"] : [UIImage imageNamed:#"tick.png"];
cell.accessoryView = [[UIImageView alloc] initWithImage:image];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL checked = [[checkedArr objectAtIndex:indexPath.row] boolValue];
[checkedArr removeObjectAtIndex:indexPath.row];
[checkedArr insertObject:(checked) ? #"FALSE":#"TRUE" atIndex:indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImage *newImage = (checked) ? [UIImage imageNamed:#"tick.png"] : [UIImage imageNamed:#"white_bg.png"];
cell.accessoryView=[[UIImageView alloc] initWithImage:newImage];
NSLog(#"Val is %i",val);
NSLog(#"selected is %#",[listArray objectAtIndex:indexPath.row]);
}
I am storing those values in database using an array checkedArr.
[dbObj insertQuestData:Question answers:listArray checkVal:checkedArr];
here dbObj is database object, others are values that i am storing to database.
Please let me know how to achieve the same.
The list array and checked array should be mutable array sent to database function which fills up the data.
self.listArray = [NSMutableArray array];
self.checkedArray = [NSMutableArray array];
[dbObj insertQuestData:Question answers:self.listArray checkVal:self.checkedArray];
// [self insertList:self.listArray andChecks:self.checkedArray];
The insertQuestData methods should fill up data like this -
- (void) insertList:(NSMutableArray *)list andChecks:(NSMutableArray *)checks
{
for (int i = 0; i < 100; i++) {
[list addObject:[NSString stringWithFormat:#"item %d", i]];
BOOL checkValue = (i % 4) == 0 ? YES : NO;
[checks addObject:[NSNumber numberWithBool:checkValue]];
}
}
The table datasource methods should be like this - (notice the commented line as correction to your code)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"CellIdentifier";//[NSString stringWithFormat:#"Cell%i",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:14.0];
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:18.0];
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// cell.textLabel.frame=CGRectMake(75.0, 50.0, 150.0, 20.0);
cell.textLabel.text=[self.listArray objectAtIndex:indexPath.row];
NSLog(#"Checked arr size %i",[self.checkedArray count]);
BOOL checked = [[self.checkedArray objectAtIndex:indexPath.row] boolValue];
UIImage *image = (!checked) ? [UIImage imageNamed:#"white_bg.png"] : [UIImage imageNamed:#"tick.png"];
cell.accessoryView = [[UIImageView alloc] initWithImage:image];
return cell;
}
This is what my sample code show on iPhone - [every 4th row is (green)ticked/selected and others are grey/unselected]
Feel free to ask if you have any specific questions / issues you face.