My app contains some UITableViews, and cells contain several views. The cells also need to load images from server, so I use the open source tool named SDWenImage to asynchronously load images.
The problem is that UITableViews scroll smoothly in simulator, but not smoothly in iOS devices. Can anybody tell me why and how to solve the problem.
Following is some code related to the above problem:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
ItemTableViewCell *cell = (ItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = (ItemTableViewCell *)[[[NSBundle mainBundle] loadNibNamed:#"ItemTableViewCell" owner:self options:nil] lastObject];
}
GroupBuy *groupBuy = [tableArray objectAtIndex:indexPath.row];
cell.groupBuy = groupBuy;
[cell refreshData];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[[NSNotificationCenter defaultCenter] addObserver:cell selector:#selector(starAction:) name:#"StarAction" object:nil];
return cell;
}
Thank you very much.
for download images asynchronously use
http://www.markj.net/iphone-asynchronous-table-image/
Related
I have tried following libraries for Image cache:
SDWebImage
UIImageView+AFNetworking
But still app getting Memory Warning, crashed and stuck while scrolling.
I have used UITableViewCell for display image.
Some of the images are 1.5 MB or more, So i am not sure whether this is the issue or anything else?
Please guide.
Any efforts from your side will be appreciated.
EDIT:
I have UITableViewCell for load UICollectionViewCell in UITableView:
Code for UITableViewCell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"homecell";
HomeViewCell *cell = (HomeViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"HomeViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSArray *arr=[[arrPsots objectAtIndex:indexPath.row ] valueForKey:#"PostImage"];
[cell setCollectionWithImageArray:arr];
return cell;
}
Code for UICollectionViewCell.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView1 cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"ScrollCollectionCell";
ScrollCollectionCell *cell = [collectionView1 dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
NSString *imgKey = (IS_IPAD?#"PostImage":#"PostImage");
NSString *strImage=[NSString stringWithFormat:#"%#",[arrImages valueForKey:imgKey]];
[cell.imgScroll setImageWithURL:[NSURL URLWithString:strImage] placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
if ([arrImages count]==1) {
self.pageControllPostImg.hidden=TRUE;
}
self.pageControllPostImg.hidden=TRUE;
self.pageControllPostImg.numberOfPages=[arrImages count];
return cell;
}
I had this problem in one project, where previous programmer create new cell every time and didn't use reusable cells.
I have a UItableview with custom cells containing textfield.
In the table view I have 5 rows, with the custom cell.
I would like to implement the accessory view (Next, previous, Done buttons) for the text fields loaded in the tableview. Please help.
Code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"CPMyTableViewCell";
CPMyTableViewCell *cell = (CPMyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CPMyTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.textVw.placeholder = #"text";
return cell;
}
You can use BSKeyboardControls
https://github.com/SimonBS/BSKeyboardControls.
Through this it is very easy to add the controls to a keyboard. You can also find example in the project.
Also have a look at this
If I want to make a 10X10 buttons in a view, the simple way is to make 100 buttons,
in Cocoa there is a NSButtonCellcan be used, so is there some thing like it in iOS?
your can create a custom cell
drag and drop a tableView cell and design with your buttons
and
add your custom tableView cell in cellForRowAtIndexPath like this,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *yourCustomeCelldentifier = #"CustomeTableCell";
yourCustomeCell *cell = (yourCustomeCell *)[tableView dequeueReusableCellWithIdentifier:yourCustomeCelldentifier];
if (cell == nil)
{
NSArray *nib;
nib = [[NSBundle mainBundle] loadNibNamed:#"CustomeCell_Nib" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
return cell;
}
hope this way will help you
I have created a custom UITableViewCell with a UISwitch, a UIStepper and two labels inside.
When I run my app in the simulator, and the tableview lists each instance of this custom cell. I notice that when I toggle the switch in the first cell and increase it's stepper (affecting one label), the ninth cell is affected the same way.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *items = [self arrayForSection:indexPath.section];
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(indexPath.section == 0){
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.notificationTitle.text = [items objectAtIndex:indexPath.row];
return cell;
}
I also have two sections in this tableview, and set the first one so that the selection style is off.
What is going on exactly and how to do I keep it from happening?
Where is the part where you are creating the custom cell? Are you doing that or is it just that you've missed it out while pasting it here?
Try this (hope you're using a NIB file to create a custom cell):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *questionTableIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:questionTableIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.notificationTitle.text = [items objectAtIndex:indexPath.row];
return cell;
}
When you use this [tableView dequeueReusableCellWithIdentifier:questionTableIdentifier]; you are actually reusing a already made instance of your cell(if there is any to be reused else create a new one). UITableViews work this way in order to conserve memory. If you have a very large number of cells it will still only consume about the same amount of memory as if there were only enough to cover the screen. In order to fix your problem you need to keep the state of your cell's some other place then the cell itself. Maybe a data structure in your tableviewcontroller or viewcontroller. And then set the values when your tableview wants to display the cell.
If you go with the non reusable cells then you could do something like this.
#property(nonatomic, strong)NSArray *cells;
- (id)init
{
self = [super init];
if ( self )
{
_cells = #[#[[[YourCell alloc] init],
[[YourCell alloc] init],
[[YourCell alloc] init]
],
[#[[YourCell alloc] init],
[[YourCell alloc] init],
[[YourCell alloc] init]]];
}
return self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return _cells[indexPath.section][indexPath.row];
}
Assuming you have 2 sections with 3 cells in each section.
When I run my ios app, only the first cell displays in the TableView, then when clicked the second cell displays. I would like them both to display at the same time, but I cannot figure out this behavior.
Here is the code for my view did load
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ProductCellId";
ProductTableCell *cell =
(ProductTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"ProductTableCell" owner:self options:nil];
cell = self.productCell;
}
Product *product = [products objectAtIndex:indexPath.row];
[cell configureForProduct:product];
return cell;
}
Any help would be much appreciated!
Since you are loading your cell from Interface builder try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ProductTableCell"];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ProductTableCell" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
cell = [topLevelObjects objectAtIndex:0];
}
Product *product = [products objectAtIndex:indexPath.row];
[cell configureForProduct:product];
return cell;
}
Change this:
static NSString *CellIdentifier = #"ProductCellId";
to:
static NSString *CellIdentifier = #"ProductTableCell";
You can find additional information for what you are trying to do in this question: Load cells from XIB files