I have two arrays1)image_array and 2)text_array.I know how to display images
and text .
cell.imageView.image = [UIImage imageNamed:[text objectAtIndex:indexPath.row]];
cell.textLabel.text = [arry objectAtIndex:indexPath.row];
it's showing like
image1 text1
image2 text2
image3 text3
|
|
image7 text7
But I need like
image1
text1
image2
text2
|
|
image7
text7
Please give me any idea.Thanks in Advanced.
What about creating a custom cell and setting their image view and label frame properties?
cell.imageView.frame = CGRectMake(XORIG, 0, WI, HI);
cell.textLabel.frame = CGRectMake(XORIG, cell.imageView.frame.origin.y + cell.imageView.frame.size.height, WL, HL);
try this
set cell style to subtitle and then
cell.imageView.image = [UIImage imageNamed:[text objectAtIndex:indexPath.row]];
cell.detailTextLabel.text = = [arry objectAtIndex:indexPath.row];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"cell %d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIImageView *myimagview = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 40, 40)];
myimagview.image = [UIImage imageNamed:#""];
[cell.contentView addSubview:myimagview];
[myimagview release];
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(10,60, 250, 20);
lbl.backgroundColor=[UIColor clearColor];
lbl.font = [UIFont fontWithName:#"ArialMT" size:14];
lbl.textColor = [UIColor grayColor];
lbl.text=#"";
[cell.contentView addSubview:lbl];
[lbl release];
cell.selectionStyle = UITableViewCellSeparatorStyleNone;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 90;
}
Related
I have to create custom UITableView and generally cell has UIImageView, and two UILabel when image of UIImageView is depend upon content which i got from server. so some cell has image and some has no image.
Problem is that when i use reusability of cell then previous UIImageView's image remain as it.. how can i remove it and implement my content on cell proper way.
Following is mine code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *isImgOfTable = [[self.listOfEvents objectAtIndex:indexPath.row] objectForKey:#"EventImage"];
NSString *CellIdentifier = #"Cell";
//NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d", indexPath.section, indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if([isImgOfTable length] > 0)
{
UIImageView *imgOfEvent = [[UIImageView alloc] init];
imgOfEvent.tag = 101;
[cell.contentView addSubview: imgOfEvent];
}
UILabel *lblEventTitle = [[UILabel alloc] init];
lblEventTitle.tag = 102;
[cell.contentView addSubview:lblEventTitle];
UILabel *lblDescription = [[UILabel alloc] init];
lblDescription.tag = 103;
[cell.contentView addSubview:lblDescription];
}
if([isImgOfTable length] > 0)
{
NSString *newPath = #"";
UIImageView *imgEvent = (UIImageView *) [cell.contentView viewWithTag:101];
imgEvent.userInteractionEnabled = YES;
newPath = [[GeneralClass getDocumentDirectory] stringByAppendingPathComponent:[[self.listOfEvents objectAtIndex:indexPath.row] objectForKey:#"EventImage"]];
imgEvent.frame = CGRectMake(mainX, 4, 45, 45);
imgEvent.backgroundColor = [UIColor colorWithRed:(187/255.f) green:(226/255.f) blue:(255/255.f) alpha:1.0f];
imgEvent.layer.cornerRadius = 7;
imgEvent.layer.borderColor = [UIColor colorWithRed:(224/255.f) green:(224/255.f) blue:(224/255.f) alpha:1.0f].CGColor;
imgEvent.layer.borderWidth = 1;
imgEvent.clipsToBounds = YES;
imgEvent.image = [UIImage imageWithContentsOfFile:newPath];
}
UILabel *lblEventTitle = (UILabel*) [cell.contentView viewWithTag:102];
.
.
// Code of UILabel *lblEventTitle
UILabel *lblEventDescription = (UILabel*) [cell.contentView viewWithTag:103];
.
.
// Code of UILabel *lblEventDescription
return cell;
}
NOTE: I must want to use reusability of cell. I dont want to fix such like
NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d", indexPath.section, indexPath.row];
And also i am not used UITableViewCell class so may be it is not possible to call/override prepareForReuse
Please give me you suggestion.
Check this code it help full to you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *isImgOfTable = [[self.listOfEvents objectAtIndex:indexPath.row] objectForKey:#"EventImage"];
NSString *CellIdentifier = #"Cell";
//NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d", indexPath.section, indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIImageView *imgOfEvent = [[UIImageView alloc] init];
imgOfEvent.tag = 101;
[cell.contentView addSubview: imgOfEvent];
UILabel *lblEventTitle = [[UILabel alloc] init];
lblEventTitle.tag = 102;
[cell.contentView addSubview:lblEventTitle];
UILabel *lblDescription = [[UILabel alloc] init];
lblDescription.tag = 103;
[cell.contentView addSubview:lblDescription];
}
UIImageView *imgEvent = (UIImageView *) [cell.contentView viewWithTag:101];
imgEvent.hidden=YES;
if([isImgOfTable length] > 0)
{
NSString *newPath = #"";
imgEvent.hidden=NO;
imgEvent.userInteractionEnabled = YES;
newPath = [[GeneralClass getDocumentDirectory] stringByAppendingPathComponent:[[self.listOfEvents objectAtIndex:indexPath.row] objectForKey:#"EventImage"]];
imgEvent.frame = CGRectMake(mainX, 4, 45, 45);
imgEvent.backgroundColor = [UIColor colorWithRed:(187/255.f) green:(226/255.f) blue:(255/255.f) alpha:1.0f];
imgEvent.layer.cornerRadius = 7;
imgEvent.layer.borderColor = [UIColor colorWithRed:(224/255.f) green:(224/255.f) blue:(224/255.f) alpha:1.0f].CGColor;
imgEvent.layer.borderWidth = 1;
imgEvent.clipsToBounds = YES;
imgEvent.image = [UIImage imageWithContentsOfFile:newPath];
}
UILabel *lblEventTitle = (UILabel*) [cell.contentView viewWithTag:102];
.
.
// Code of UILabel *lblEventTitle
UILabel *lblEventDescription = (UILabel*) [cell.contentView viewWithTag:103];
.
.
// Code of UILabel *lblEventDescription
return cell;
}
Might i suggest subclassing UITableViewCell and using prepareForReuse
-(void)prepareForReuse
{
labelOne = #"Default text 1";
labelTwo = #"Default label 2";
[imageView setImage:nil]; //Or you could set a default image?
}
I'm trying to change the cell background image when user tap on a cell (highlighted state), I've been trying this way but it's not working:
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.textLabel.textAlignment = NSTextAlignmentRight;
cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
cell.textLabel.font = [UIFont fontWithName:kFONT_NAME size:kFONT_SIZE];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"row320x63.png"] highlightedImage:[UIImage imageNamed:#"row320x63_pressed.png"]];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"row320x63.png"]];
cell.textLabel.text = [self.listOfMenuSettings objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"settings_icon_%d", indexPath.row]];
UIImageView *pressed = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"row320x63_pressed.png"]];
[cell setSelectedBackgroundView:pressed];
return cell;
}
What I'm missing?
I don't find setSelectedBackgroundView: instance method in apple documentation. But there is a property selectedBackgroundView.So try:
cell.selectedBackgroundView = pressed;
I need to display the cell data. Actually if I debug the code I can see that the cell.label.text does have correct value, but nothing is getting displayed on the simulator. I have set the row count in numberOfRowsInSection method.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell";
LogTableCell *cell = (LogTableCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
CGRect frame1 = [ self.view frame];
frame1.origin.x=22.0f;
frame1.origin.y=55.0f;
frame1.size.width = 298.0f;
frame1.size.height= 425.0f;
[self.view.superview setFrame:frame1];
int btnWidth = ((self.view.frame.size.width-16)/3);
if(cell == nil)
{
cell = [[LogTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
LogItem *lg = [[LogItem alloc]init];
lg = [arrLogs objectAtIndex:indexPath.row ];
NSLog(#"%d",indexPath.row);
cell.lblname.textColor=[UIColor whiteColor];
cell.lblsubtype.textColor=[UIColor whiteColor];
cell.lbltime.textColor=[UIColor whiteColor];
cell.lblname.font=[UIFont fontWithName:#"Merriweather" size:15.0];
cell.lblsubtype.font=[UIFont fontWithName:#"Merriweather-Light" size:13.0];
cell.lbltime.font=[UIFont fontWithName:#"Merriweather-Light" size:13.0];
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(1, cell.contentView.frame.size.height, 298.0,2)] ;
imageview.image = [UIImage imageNamed: #"lineseperator.png"];
[cell.contentView addSubview:imageview];
cell.lblname.text = lg.name;
NSLog(#"%#",lg.name);
[[cell imgtype] setImage:[UIImage imageNamed:lg.imagetype]];
if([lg.subtype length] == 0){
NSLog(#"%#",lg.subtype);
cell.lblsubtype.text = lg.time;
cell.lbltime.text = #"";
}
else{
NSLog(#"%#",lg.subtype);
cell.lblsubtype.text = lg.subtype;
cell.lbltime.text = lg.time;
}
return cell;
}
The imageview you're adding to the cell is covering all other content. You'll need to either resize/reposition it, or just use the already existing cell.imageView instead.
As I scroll down the list, all the rows are there, but they keep adding more subviews the more they appear on the visible frame
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuse = #"RuleCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:reuse];
}
NSUInteger row = indexPath.row;
[self createCell: cell onRow: row];
return cell;
}
- (void) createCell: (UITableViewCell*)cell onRow: (NSUInteger)row
{
UIImageView* bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell_background_spade_active.png"]];
cell.backgroundView = bgImage;
cell.textLabel.hidden = YES;
UILabel* titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(100, CGRectGetHeight(cell.frame) / 2, 200, 50)];
titleLabel.text = [[self.ruleList objectAtIndex: row] objectForKey: TitleKey];
titleLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview: titleLabel];
}
I think you need to execute almost all of the logic that's in createCell: only within the if (cell == nil){ segment of your code. The part that should execute where you're currently calling createCell: is just getting a reference to the titleLabel and setting its text value.
To clarify, here's the kind of modification I'm suggesting (not tested, but should give the right idea):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuse = #"RuleCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:reuse];
[self setUpCell: cell];
}
NSUInteger row = indexPath.row;
UILabel *titleLabel = (UILabel *)[cell.contentView viewWithTag:42];
titleLabel.text = [[self.ruleList objectAtIndex: row] objectForKey: TitleKey];
return cell;
}
- (void) setUpCell: (UITableViewCell*)cell
{
UIImageView* bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell_background_spade_active.png"]];
cell.backgroundView = bgImage;
cell.textLabel.hidden = YES;
UILabel* titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(100, CGRectGetHeight(cell.frame) / 2, 200, 50)];
titleLabel.tag = 42;
titleLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview: titleLabel];
}
I have a standard tableview here and I wanted to be able to add a space between each cell that is invisible/hidden that shows the background. What is the correct way of doing it?
Here is the code I am using:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
// Format each frame that holds each piece of content in each row
CGRect countyImageFrame = CGRectMake(275, 6, 18, 18);
UIImageView *countyImageLabel = [[UIImageView alloc] initWithFrame:countyImageFrame];
countyImageLabel.tag = 0016;
countyImageLabel.backgroundColor = BG_COLOR
[cell.contentView addSubview:countyImageLabel];
CGRect callTypeImageFrame = CGRectMake(275, 30, 18, 18);
UIImageView *callTypeImageLabel = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
callTypeImageLabel.tag = 0017;
callTypeImageLabel.backgroundColor = BG_COLOR
[cell.contentView addSubview:callTypeImageLabel];
CGRect callTypeFrame = CGRectMake(5, 2, 175, 15);
UILabel *callTypeLabel = [[UILabel alloc] initWithFrame:callTypeFrame];
callTypeLabel.tag = 0011;
callTypeLabel.backgroundColor = BG_COLOR
callTypeLabel.numberOfLines = 2;
callTypeLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:callTypeLabel];
CGRect locationFrame = CGRectMake(5, 21, 175, 10);
UILabel *locationLabel = [[UILabel alloc] initWithFrame:locationFrame];
locationLabel.tag = 0014;
locationLabel.backgroundColor = BG_COLOR
locationLabel.numberOfLines = 2;
locationLabel.font = [UIFont systemFontOfSize:10];
[cell.contentView addSubview:locationLabel];
CGRect unitsFrame = CGRectMake(3, 40, 175, 10);
UILabel *unitsLabel = [[UILabel alloc] initWithFrame:unitsFrame];
unitsLabel.tag = 0012;
unitsLabel.backgroundColor = BG_COLOR
unitsLabel.textColor = [UIColor blackColor];
unitsLabel.font = [UIFont italicSystemFontOfSize:10];
[cell.contentView addSubview:unitsLabel];
CGRect stationFrame = CGRectMake(185 , 28, 85, 20);
UILabel *stationLabel = [[UILabel alloc] initWithFrame:stationFrame];
stationLabel.tag = 0013;
stationLabel.backgroundColor = BG_COLOR
stationLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:stationLabel];
CGRect callnumberFrame = CGRectMake(185 , 5, 80, 20);
UILabel *callnumberLabel = [[UILabel alloc] initWithFrame:callnumberFrame];
callnumberLabel.tag = 0015;
callnumberLabel.backgroundColor = BG_COLOR
callnumberLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:callnumberLabel];
}
// Display content in each cell
if ([currentCall.county isEqualToString:#"W"]) {
UIImage *countyImage = [UIImage imageNamed:#"blue.png"];
UIImageView *countyImageLabel = (UIImageView *)[cell.contentView viewWithTag:0016];
countyImageLabel.image = countyImage;
}
else {
UIImage *countyImage = [UIImage imageNamed:#"green.png"];
UIImageView *countyImageLabel = (UIImageView *)[cell.contentView viewWithTag:0016];
countyImageLabel.image = countyImage;
}
if ([currentCall.callType isEqualToString:#"F"]) {
UIImage *callTypeImage = [UIImage imageNamed:#"red.png"];
UIImageView *callTypeImageLabel = (UIImageView *)[cell.contentView viewWithTag:0017];
callTypeImageLabel.image = callTypeImage;
}
else {
UIImage *callTypeImage = [UIImage imageNamed:#"yellow.png"];
UIImageView *callTypeImageLabel = (UIImageView *)[cell.contentView viewWithTag:0017];
callTypeImageLabel.image = callTypeImage;
}
UILabel *callTypeLabel = (UILabel *)[cell.contentView viewWithTag:0011];
callTypeLabel.text = [currentCall currentCallType];
UILabel *locationLabel = (UILabel *)[cell.contentView viewWithTag:0014];
locationLabel.text = [currentCall location];
UILabel *unitsLabel = (UILabel *)[cell.contentView viewWithTag:0012];
unitsLabel.text = [currentCall units];
NSString *station = [#"Station: " stringByAppendingString:currentCall.station];
UILabel *stationLabel = (UILabel *)[cell.contentView viewWithTag:0013];
stationLabel.text = station;
NSString *callnumber = [#"Call No: " stringByAppendingString:currentCall.callnumber];
UILabel *callnumberLabel = (UILabel *)[cell.contentView viewWithTag:0015];
callnumberLabel.text = callnumber;
cell.backgroundColor = BG_COLOR
return cell;
}
// Set how tall each cell is
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 55;
}
Let me start by saying I did not read all your code. That said I have done exactly this. The first approach is to declare in the heightForCell a larger value for the cell above where the gap should be. The cell.contentView might need to have its clipToBounds set. I believe in this case the cell size should be set by setting the cell.frame.size (the origin is 0,0), and the contentsView and backgroundview properly sized too.
The second way to do it is to insert empty cells - cells with a different reuse identifier, and which are just empty cells with everything set to have a clear color.
Just use the trick to have spaces between each cells..
static NSString *CELL_ID2 = #"SOME_STUPID_ID2";
// even rows will be invisible
if (indexPath.row % 2 == 1)
{
UITableViewCell * cell2 = [tableView dequeueReusableCellWithIdentifier:CELL_ID2];
if (cell2 == nil)
{
cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CELL_ID2];
[cell2.contentView setAlpha:0];
[cell2 setUserInteractionEnabled:NO]; // prevent selection and other stuff
}
return cell2;
}
I just use different heights and alternate colours between cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
if (indexPath.row % 2 == 1)
{
static NSString *CellIdentifier = #"cellID1";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.backgroundColor = [UIColor colorWhite];
return cell;
} else {
static NSString *CellIdentifier2 = #"cellID2";
UITableViewCell *cell2 = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell2 == nil) {
cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier2];
}
cell2.backgroundColor = [UIColor clearColor];
return cell2;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 1) {
return 40.0;
} else {
return 2.0;
}
}