I have created cells in UITableView. Cells are generating dynamically from array, it fits into cells properly too.
But when I scroll down and again scroll up my old values of same array get replaced with last values of same array.
I want all values of array in sequence depending on scrolling up or down. Below is my code, please let me know where I am making mistake...
static NSString *simpleTableIdentifier = #"SimpleTableCell";
LineDetailCustomCell *cell = (LineDetailCustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"LineDetailCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
return cell;
Please find images with question description
This second image come when I scroll down, its working fine for scroll down.
But when I scroll up labels get overridden with new values instead of as shown in first image. That means label 01 , 02 , 03 ... replace with next values
Assuming that you have a property of type NSMutableArray named items and it has data in it (which are strings)
#property (strong, nonatomic) NSMutableArray *items;
Then in your tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"SimpleTableCell";
LineDetailCustomCell *cell = (LineDetailCustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (!cell) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"LineDetailCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSString *item = [self.items objectAtIndex:indexPath.row];
cell.titleLabel.text = item;
return cell;
}
Make sure that the reuse identifier for LineDetailCustomCell is set as SimpleTableCell from the Xib. This is needed for recycling the cells.
Try this :
NSString *cellIdentifier = [NSString stringWithFormat:#"cell%d",indexPath.row];
//static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
NSArray *views=[cell.contentView subviews];
for(int i=0;i<[views count];i++)
{
UIView *tempView=[views objectAtIndex:i];
[tempView removeFromSuperview];
tempView = nil;
}
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
}
Related
I am new to developing. i have some experience but i'm not great at it (yet). I'm working on this tableview app which shows data. I want to show that data in a custom cell. But i don't know how to do that.
This is the code i use for my tableview:
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"HistoryViewTableCell"];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"HistoryViewTableCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
NSDictionary * tempDictionary = [tableData objectAtIndex:indexPath.row];
cell.textLabel.text = [tempDictionary objectForKey:#"PickupAddress"];
return cell;
}
I need to show this line of code in my custom cell.
cell.textLabel.text = [tempDictionary objectForKey:#"PickupAddress"];
I already made a class and a xib file for the custom cell
This may solve the problem:
HistoryTableViewCell *cell = (HistoryTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"HistoryViewTableCell"];
your written kind of code will not work. if you are using custom XIB than add a UILable in the xib. and than assign them tag number.
now from ViewController do the following:
write this after #implementation
enum {
CELL_TITLE = 2,
CELL_SUBTITLE_1 = 3,
};
Load the Custom XIB File in cellforrowatindexpath method.
// Get table View Cell
static NSString *CellIdentifier = #"customCell";
UITableViewCell *cell = [atableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib;
nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = (UITableViewCell *) [nib objectAtIndex:0];
}
Now Add text on Lable with the below methods;
UILabel *titleLable = (UILabel *) [cell.contentView viewWithTag:CELL_TITLE];
titleLable.text = #"This is your text";
I would like to change the format of the first cell in my UITableView (masterViewController), so that the image stretches across thewidth of the whole cell and the text label is shown above the image. Also would like to add a second text label to this cell only?
Any help on how to do this would be great, I have looked without success for this particular problem and I am fairly new to UITableViews.
Make one custom cell as per your requirement and load it for indexpath.row== 0 and simple UITableViewCell for all other cells. hope it will helps you
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"CustomCell";
CustomCell *customcell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
static NSString * identifier = #"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(indexPath.row == k)
{
if(customcell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
scrlcell = [nib objectAtIndex:0];
}
}
else{
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
}
if(indexPath.row == k)
return customcell;
else
return cell;
}
You should build a custom cell (you can build it in Interface builder) and then use it on your indexPath.row == 0. Like this:
static NSString *CellIdentifier = #"CellIdentifier";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
NSArray *topLevel = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:nil options:nil];
for (id currentObject in topLevel){
if ([currentObject isKindOfClass:[CustomCell class]])
{
cell = currentObject;
break;
}
}
}
if(indexPath.row == 0){
// Use your cell here
}else{
// Build a new cell
}
return cell;
I'm getting an error in the below code. ..L1name , L1email , L1code are the property names for the array nameData, nameEmail, nameCode.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
SimpleList1 *cell = (SimpleList1 *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.L1name.text = [nameData objectAtIndex:indexPath.row];
cell.L1email.text = [nameEmail objectAtIndex:indexPath.row];
cell.L1code.text = [nameCode objectAtIndex:indexPath.row];
return cell;
}
I guess there is a typo in your question title and the error is actually referencing UITableViewCell. This would be because UITableViewCell doesn't have your custom properties.
You need to ensure you have created an instance of your custom cell subclass, and that the variable is of the correct class type so the compiler knows what properties are available (casting if required):
MYCell *cell = (MYCell *)[tableView dequeueReusableCellWithIdentifier:...];
I really can't understand why i get a "EXC_BAD_ACCESS" in the line where I assign NSArray *topLevelObjects. It's crazy because I use exactly the same code and the same BlogCell in another tabliView, and there it's working perfectly!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int r = indexPath.row;
static NSString *CellIdentifier = #"Blog";
BlogCell *cell = (BlogCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"BlogCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
return cell;
}
Make sure all the outlets you specify in BlogCell to FilesOwner have corresponding IBOutlets in this class.
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