UITableView doesn't show images - ios

It appears my tableview delegate won't show my image at all. The image shows fine on my UIImageView that I added to my view controller. I am using the table view from interface builder if this bit of information helps.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView setDelegate:self];
[tableView setDataSource:self];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.accessoryView = [[UIImageView alloc] initWithImage:myimage];
return cell;
}

Try setting the delegate and data source via interface builder, Or in the viewDidLoad method.
-(void)viewDidLoad{
self.tableView.delegate = self;
self.tableView.dataSource = self;
//...
}
Also, make sure that the myImage object is not nil
If i can remember correctly, default style has an imageView property. Try:
cell.imageView.image = [UIImage imageNamed:#"image.png"];

You need to set the delegate and datasource in your viewDidLoad method and within your .h file in stead of the delegate method of the tableview itself.
EDIT:
Also make sure that all your delegates methods are added
Those delegates are:
// Return the number of sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// Return the number of rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
//filling of tableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}
//pressing of a row
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

Related

I have created a table view programmatically as follows-

- (void)viewDidLoad {
[super viewDidLoad];
menuArray=[[NSMutableArray alloc]initWithObjects:#"Address",#"Restaurants",#"Deals",#"Orders",#"Account",#"Address Book",#"Settings",#"Live Chat",#"Info",nil];
tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, menuSubView.frame.size.height, self.view.frame.size.width-80, self.view.frame.size.height-menuSubView.frame.size.height)];
tableView.delegate=self;
tableView.dataSource=self;
[self.view addSubview:tableView];
}
and other methods as follows-
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return menuArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reusableIdentifier=#"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusableIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusableIdentifier];
}
cell.textLabel.text=[menuArray objectAtIndex:indexPath.row];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 70;
}
However when I run the program i am able to see the table view as desired. But when I scroll through it, table's data disappear and only blank table view remains. Can anyone help me? Thanks in advance.
--> I appreciate above answer.
--> Please try this line in "cellForRowAtIndexPath" Method...!
--> Sometimes it happens. I have solved this issue with this line in past. I hope it's useful for you.
UITableViewCell *cell = [self.tblView dequeueReusableCellWithIdentifier:#"Cell4" forIndexPath:indexPath];

adding table cells to table in a specific section in iOS

Please if you are going to add the link of the documentation, please don't do that
I have a problem her and i don't know how to solve it and need a clear answer
I have build a table view from xcode UI and add 5 sections each section contains some cells except section2, what I want to do is to add cells to section2 in viewDidLoaded is that possible or not
this is the header file
#interface Menu : UITableViewController;
this is the implementation file
#implementation Menu
#synthesize drawerTableView;
- (void) viewDidLoad {
NSDictionary *object = [drawerTableView dataSource];
KhawaterDataManager *sharedManager = [KhawaterDataManager instance];
CategoriesResponse *categories = [sharedManager categories];
[categories printObject];
}
/*- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"%d", section);
switch (section) {
case 1:
return 5;
break;
}
return [tableView.dataSource[section] count];
}*/
/*- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[cell setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
cell.textLabel.text = #"hello";
return cell;
}*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath{
// my logic
}
#end
I think you are using static cells in your UITableView. What you can do is make use of dynamic tableview cells. You have to uncomment and use both methods to create your cell:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

UITableView does not load data

I am relative new to iOS development. Right now, I created 2 ViewController using storyboard. The one consist one button that lead into another controller using segue (show).
This controller is TableViewController that embedded in Navigation Controller and already connected with its responsible class that inherit from UITableViewController.
My problem is this page doesn't load its data (NSMutableArray) that already initialized in viewDidLoad using
_dataClient = [NSMutableArray arrayWithObjects:#"First City",#"Second City",#"Third City",#"Forth City",#"Fift City", nil];
This is my table delegate and datasource:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_dataClient count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = #"cellItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
// Configure the cell...
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [_dataClient objectAtIndex:indexPath.row];
return cell;
}
Is there any step that I forgot to do?
You must return 1 section at least. Change this line:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1; /// here
}

UITableview tries to scrollToTop but doesn't (Indicator tries to go up, strange.) - video

So I've been working on a UITableView and I can't hit the statusbar to scroll up, however it seems like it's trying to scroll up, but it doesn't.
Here you can see a video of it (Decided to use mp4 instead of GIF since it was so laggy)
http://i.gyazo.com/4c7be7437116e420411ac879b6d0d784.mp4
If you look at the video you can see that you indicator on the right side tries to scroll up but doesn't ? Has anyone experienced something similar ?
I'm also using ECSlidingViewController
My current code
Called upon -viewDidLoad
-(void)setTableViewStuff {
[self.myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[self.myTableView registerNib:[UINib nibWithNibName:#"YouTubeTableCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
self.myScrollView.scrollsToTop = NO;
self.myTableView.scrollsToTop = YES;
}
Table View Code
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 256;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return myVideoTitles.count;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
YouTubeTableCell *cell = (YouTubeTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (indexPath.section == 0) {
NSString* thumbnailURL = myVideoThumbnails[indexPath.row];
[cell.myThumbnail setImageWithURL:[NSURL URLWithString:[thumbnailURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:nil];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return NO;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
EDIT:
Found a solution to this!!, Apperiantely if you're using ECSlidingViewController you must have yourtableview.scrollstotop = NO; inside the "drawer"
Found a solution to this!!, Apperiantely if you're using ECSlidingViewController you must have yourtableview.scrollstotop = NO; inside the "drawer"

iOS: uitableview redraw objects on first cell to bottom cells

i have a uitableview with custom cells.. with normal code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
DDMainCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[DDMainCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
}
the problem is when i select one cell i add progress bar on the cell that download data online.. but when i Scroll down i find that every 10 cells have the same progress bar .. how can i prevent this behavior ?
Try this,
- (void)viewDidLoad
{
[super viewDidLoad];
dataarr=[[NSMutableArray alloc]init];
indexarr=[[NSMutableArray alloc]init];
mytableview=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
mytableview.dataSource=self;
mytableview.delegate=self;
[self.view addSubview:mytableview];
for (int i=0; i<30; i++) {
[dataarr addObject:[NSString stringWithFormat:#"%d",i]];
}
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [dataarr 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] ;
}
cell.textLabel.text=[dataarr objectAtIndex:indexPath.row];
UIActivityIndicatorView *act=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[act setFrame:CGRectMake(50, 20, 20, 20)];
act.hidden=YES;
[cell.contentView addSubview:act];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
if ([indexarr containsObject:[dataarr objectAtIndex:indexPath.row]])
{
[act startAnimating];
act.hidden=NO;
return cell;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexarr containsObject:[dataarr objectAtIndex:indexPath.row]])
{
[mytableview reloadData];
return;
}
[indexarr addObject:[dataarr objectAtIndex:indexPath.row]];
[mytableview reloadData];
}
Make sure, when downloading is complete, then remove this object from indexarr....
That's because your cells are getting reused; UITableView will put off-screen cells into the reusable cell queue, and dequeue them for reuse if the reuseIdentifier matches. You should use some other data structure (e.g. NSArray or NSDictionary) to track which indices/cells have already been tapped. Then, in the method you showed above, regardless of whether the cell was init-ed or dequeued, set the progress bar according to your underlying data structure.
Here your used UITableViewCellIdentifier is reuseIdentifier. Which will work for all Cells are same type. Now your are taking once cell with progress bar. Now it will different from all cells data.
So use one more tableview cell for progress bar, or while reloading table remove the Progress bar which is exists already and add again. for this use tag for progress bar.

Resources