When I set the red part on the top cell ,the content of next part which is the green part doesn't display completely or doesn't display.Only when I select the green part ,the image will appear .If I put the one cell of green above the red part ,it will display and the next part which is under the red part doesn't appear.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0) {
static NSString *identifyCell = #"TopCell";
TopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifyCell];
if (!cell) {
cell = (TopTableViewCell *)[[[NSBundle mainBundle] loadNibNamed:#"TopTableViewCell"
owner:self options:nil] objectAtIndex:0];
//cell.lbValue.hidden = YES;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//config the cell
return cell;
}
else{
static NSString *identifyCell = #"HomeCell";
HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifyCell];
if (!cell) {
cell = (HomeTableViewCell *)[[[NSBundle mainBundle] loadNibNamed:#"HomeTableViewCell"
owner:self options:nil] objectAtIndex:0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//cell.lbValue.hidden = YES;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
cell.homeimg.image = [UIImage imageNamed:#"home1.jpg"];
//config the cell
return cell;
}
}
Related
Normally in iOS, the cells in a tableview are loading on scrolling. In this time I'm setting a label text. In this case I'm setting this text dynamically. When I'm scrolling down it's working as I expected. But when It's scrolling up, it's not working as expected. Here what I tried.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"moduleCell";
DateTableViewCell *cell = (DateTableViewCell*)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"DateTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#""]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (self.newsSegmentControl.selectedSegmentIndex == 1) {
[cell.dateView setHidden:NO];
}else{
[cell.dateView setHidden:YES];
}
NSString *day = [self getDayLabel:date];
if ([dateReturned isEqualToString:day]) {
[cell.lblDate setText:#""];
if (self.newsSegmentControl.selectedSegmentIndex == 1) {
[cell.dateView setHidden:YES];
}
}else{
[cell.lblDate setText:day];
dateReturned = day;
}
}
How may I avoid re-draw the label text in cell on scrolling?
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 have one table that I want load from xib two cell.
in UITableViewCell xib file I have two cell (two part with tag 100 and 200) that I want load first view (first cell) in first row and load second cell in second row.
both cells are in xib file like this:
this is my code :
#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
CustomCell *cell = (CustomCell *)[self.table dequeueReusableCellWithIdentifier:#"myCell"];
if (cell == nil)
{
NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:nil options:nil];
NSLog(#"%#",topLevelObject);
cell = [topLevelObject objectAtIndex:0];
NSLog(#"%#",cell);
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
else if (indexPath.section == 1) {
CustomCell *cell2 = (CustomCell *)[self.table dequeueReusableCellWithIdentifier:#"Cell2"];
if (cell2 == nil)
{
NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:nil options:nil];
for (id currentObject in topLevelObject) {
if ([currentObject isKindOfClass:[CustomCell class]]) {
cell2 = (CustomCell *)currentObject;
break;
}
}
}
cell2.selectionStyle = UITableViewCellSelectionStyleNone;
return cell2;
}
return nil;
}
I don't know where is my wrong because when run code I see only first cell in tableView
You are using first cell from XIB at section = 0
cell = [topLevelObject objectAtIndex:0];
so, in the same way you can get the second cell at index 1 for the section 1
cell = [topLevelObject objectAtIndex:1];
In cellForRowAtIndexPath I am adding a UIImageView to cell.contentView. The problem is that when the cell scrolls off the screen and back on, it adds the same image again on top of the one that is already there. This happens continuously until I get a very stacked-up blurry image.
Do you have to keep removing any image views that you add to cell.contentView? If so, in what delegate method do you do that in?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
MyTableCell *cell = (MyTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.jpg"]];
imageView.center = CGPointMake(310, 48);
[cell.contentView addSubview:imageView];
[imageView release];
return cell;
}
If you don't want to keep putting imageViews in your cell, you have to do all the customization inside the if(cell==nil) block, otherwise it will add one every time a cell recycles. When using cell recycling you always want to keep anything that is consistent for all of your cells in that block so that they only get added once.
Ex:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
MyTableCell *cell = (MyTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
//Add custom objects to the cell in here!
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.jpg"]];
imageView.center = CGPointMake(310, 48);
[cell.contentView addSubview:imageView];
[imageView release];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
If each cell should need a different image, you can try something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
static const int ImageViewTag = 1234; //any integer constant
MyTableCell *cell = (MyTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIImageView *imageView;
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
//Add custom objects to the cell in here!
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, imgWidth, imgHeight)];
imageView.center = CGPointMake(310, 48);
imageView.tag = ImageViewTag;
[cell.contentView addSubview:imageView];
[imageView release];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
imageView = [cell viewWithTag:ImageViewTag];
}
imageView.image = yourUIImageForThisCell;
return cell;
}