cell specification in uitableview [duplicate] - objective-c

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
UITableView create 4 square
I want to have 4 cell in one row in uiTableview but nothing appear, I don't know why would you please help me
this a the picture for cell
Thanks in advance
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];
NSString *monKey = #"Monday";
NSString *tueKey = #"Tuesday";
NSString *wedKey = #"Wednday";
NSString *thuKey = #"Thusday";
NSString *friKey = #"Friday";
NSString *satKey = #"Satuarday";
NSString *sunKey = #"Sunnday";
[contents setObject:[NSArray arrayWithObjects:#"Work Time", #"Absence", nil] forKey:monKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", #"Absence", nil] forKey:wedKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:tueKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:thuKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:friKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:satKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:sunKey];
[keys addObject:tueKey];
[keys addObject:monKey];
[keys addObject:wedKey];
[keys addObject:thuKey];
[keys addObject:friKey];
[keys addObject:satKey];
[keys addObject:sunKey];
[self setSectionKeys:keys];
[self setSectionContents:contents];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:#selector(addNewItem)];
self.navigationItem.rightBarButtonItem = rightButton;
}
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
int column = 4;
for (int i=0; i<column; i++) {
UIImageView *aBackgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(32+184*i,10, 167,215)];
aBackgroundImageView.tag = (column*indexPath.row)+i;
[cell.contentView addSubview:aBackgroundImageView];
// [aBackgroundImageView release];
}
return cell;
}
Edit 1
this is the picture when I added this line
aBackgroundImageView.backgroundColor = [UIColor redColor];
Edit 2 :
with Rog method

It seems to me that in:
for (int i=0; i<column; i++) {
UIImageView *aBackgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(32+184*i,10, 167,215)];
aBackgroundImageView.tag = (column*indexPath.row)+i;
[cell.contentView addSubview:aBackgroundImageView];
// [aBackgroundImageView release];
}
you are not providing a content for your views...
so that should explain why you don't see anything... Try adding:
aBackgroundImageView.backgroundColor = [UIColor redColor];
and see if anything changes.

Here i create the one demo application like you want and that some of the code put into here.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSUInteger devide=[self.tableArray count] / 3;
if ([self.tableArray count] % 3 >0 ) {
devide+=1;
} else {
devide=devide;
}
return devide;
}
And cellForRowAtIndexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil){
cell=[[[NSBundle mainBundle] loadNibNamed:#"CCell" owner:self options:nil] objectAtIndex:0];
UIButton *imgV1=(UIButton*)[cell viewWithTag:1];
UIButton *imgV2=(UIButton*)[cell viewWithTag:2];
UIButton *imgV3=(UIButton*)[cell viewWithTag:3];
[imgV1 addTarget:self action:#selector(btnTapped:) forControlEvents:UIControlEventTouchDown];
[imgV2 addTarget:self action:#selector(btnTapped:) forControlEvents:UIControlEventTouchDown];
[imgV3 addTarget:self action:#selector(btnTapped:) forControlEvents:UIControlEventTouchDown];
HJManagedImageV *img1=[[HJManagedImageV alloc] initWithFrame:CGRectMake(0, 0, imgV1.frame.size.width, imgV1.frame.size.height)];
HJManagedImageV *img2=[[HJManagedImageV alloc] initWithFrame:CGRectMake(0, 0, imgV2.frame.size.width, imgV2.frame.size.height)];
HJManagedImageV *img3=[[HJManagedImageV alloc] initWithFrame:CGRectMake(0, 0, imgV3.frame.size.width, imgV3.frame.size.height)];
img1.tag=img2.tag=img3.tag=10000;
img1.backgroundColor=img2.backgroundColor=img3.backgroundColor=[UIColor clearColor];
[imgV1 addSubview:img1];
[imgV2 addSubview:img2];
[imgV3 addSubview:img3];
}
int x=indexPath.row;
x=x*3;
UIButton *imgV1=(UIButton*)[cell viewWithTag:1];
UIButton *imgV2=(UIButton*)[cell viewWithTag:2];
UIButton *imgV3=(UIButton*)[cell viewWithTag:3];
HJManagedImageV *img1=(HJManagedImageV*)[imgV1 viewWithTag:10000];
HJManagedImageV *img2=(HJManagedImageV*)[imgV2 viewWithTag:10000];
HJManagedImageV *img3=(HJManagedImageV*)[imgV3 viewWithTag:10000];
[img1 clear];
[img2 clear];
[img3 clear];
[imgV1 setTitle:[self.tableArray objectAtIndex:x] forState:UIControlStateDisabled];
img1.url=[NSURL URLWithString:[self.tableArray objectAtIndex:x]];
[img1 showLoadingWheel];
[GlobalCacheManager manage:img1];
if((x+1) >= self.tableArray.count){
imgV2.hidden=imgV3.hidden=YES;
[imgV2 setBackgroundImage:nil forState:UIControlStateNormal];
[imgV3 setBackgroundImage:nil forState:UIControlStateNormal];
} else if( (x+2)>=self.tableArray.count) {
img2.url=[NSURL URLWithString:[self.tableArray objectAtIndex:x+1]];
[img2 showLoadingWheel];
[GlobalCacheManager manage:img2];
imgV1.hidden=imgV2.hidden=YES;
imgV3.hidden=YES;
[imgV2 setTitle:[self.tableArray objectAtIndex:x+1] forState:UIControlStateDisabled];
[imgV3 setBackgroundImage:nil forState:UIControlStateNormal];
} else {
[img2 showLoadingWheel];
[img3 showLoadingWheel];
img2.url=[NSURL URLWithString:[self.tableArray objectAtIndex:x+1]];
[GlobalCacheManager manage:img2];
img3.url=[NSURL URLWithString:[self.tableArray objectAtIndex:x+2]];
[GlobalCacheManager manage:img3];
imgV2.hidden=imgV3.hidden=NO;
[imgV2 setTitle:[self.tableArray objectAtIndex:x+1] forState:UIControlStateDisabled];
[imgV3 setTitle:[self.tableArray objectAtIndex:x+2] forState:UIControlStateDisabled];
}
return cell;
}

Your method is a bit of a mess. Your main problem is how you are setting the frame origin for the UIImageView instances as they are 1) too big for the cell 2) out of the cell bounds.
Try this (ps: there may be typos as I've written it off the top of my head):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
int numberOfColumns = 4;
CGSize gridSize = CGSizeMake(cell.contentView.frame.size.width / numberOfColumns, cell.contentView.frame.size.height / 2); // Height size is arbitrary here, width is obtained by dividing total width by number of columns
for (int i=0; i < column; i++)
{
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(gridSize.width * i, cell.contentView.frame.size.height/2 - gridSize.height/2, gridSize.width, gridSize.height)];
aview.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1/i+1]; // This is just so you can see the different gridCells within the cell
[cell.contentView addSubview:aView];
[aView release];
}
return cell;
}

Related

Adding label color close to each row in UITableView Objective-C

I have a UITableView with different sections and rows. I want to have different label or image like a icone close each cell but I don't know why it's looks like this picture:
Also scrolling changed the size of labels!
Here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
if(indexPath.row == 0)
{
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
label.backgroundColor = [UIColor darkGrayColor];
[cell.contentView addSubview:label];
}
else if(indexPath.row == 1)
{
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
label.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:label]; }
else if(indexPath.row == 2)
{
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
label.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:label]; }
else{
}
[[cell textLabel] setText:contentForThisRow];
return cell;
}
In your code when user scroll table, labels created many times! You must check if color label already exist in cell and then set color
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
int colorLabelTag = 9999;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Because table cells are reusing we must check is label already in cell
UILabel* label = (UILabel *) [cell viewWithTag:colorLabelTag];
// If no - we create label
if (!label) {
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
label.tag = colorLabelTag;
[cell addSubview:label];
}
if(indexPath.row == 0)
{
label.backgroundColor = [UIColor darkGrayColor];
}
else if(indexPath.row == 1)
{
label.backgroundColor = [UIColor redColor];
}
else if(indexPath.row == 2)
{
label.backgroundColor = [UIColor redColor];
}
[[cell textLabel] setText:contentForThisRow];
return cell;
Unless you want more cusotomizing than this there is no need for subclassing the cell.
Let me give it a try:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
// If you need to add more custom views then create them here. E.g.:
UILabel *myLable = [[UILabel alloc] initWithFrame:CGRectMake(10,200,20,20)];
myLable.text = #"Some Value";
label.tag = 101; // use anything but 1. Better use a constant expression.
[cell.contentView addSubview:myLable];
}
switch (indexPath.row) {
case 1:
cell.imageView.image = [UIImage imageNamed:#"red.png"];
break;
case 2:
cell.imageView.image = [UIImage imageNamed:#"blue.png"];
break;
case 3:
cell.imageView.image = [UIImage imageNamed:#"green.png"];
break;
}
UILabel* theLabel = (UILabel *) [cell viewWithTag:101]; // again, a constant is of better style here.
if (theLabel { // it must exist. However, it is good practice to check upon that.
label.text = #"someOtherValue";
}
[[cell textLabel] setText:contentForThisRow];
// you may need to layout your subviews here.
return cell;
}

removeObjectAtIndex crashing the app

In the .h file I have set the below line.
NSMutableArray *repArray;
In the .m file I have placed the below lines.(I put just the required lines of code for this ex)
-(void)read_data_fromDB
{
objectsForCharacters = [[NSMutableDictionary alloc]init];
sqlite3 *db = [contactAppDelegate getNewDBConnection];
NSMutableString *query = nil;
query = [NSMutableString stringWithFormat:#"select representative, title, business_name, uid from corporate where profiler_type <> 'OWN_CORPORATE' order by representative asc"];
const char *sql = [query UTF8String];
sqlite3_stmt *selectAllStmt = nil;
if(sqlite3_prepare_v2(db,sql, -1, &selectAllStmt, NULL)!= SQLITE_OK)
NSAssert1(0,#"error preparing statement",sqlite3_errmsg(db));
else
{
repArray = [[NSMutableArray alloc]init];
businessArray = [[NSMutableArray alloc]init];
titleArray = [[NSMutableArray alloc]init];
uidArray = [[NSMutableArray alloc]init];
while(sqlite3_step(selectAllStmt)==SQLITE_ROW)
{
char *chrstr =(char *)sqlite3_column_text(selectAllStmt, 0);
if(chrstr !=NULL)
{
corpRepresentative = [NSString stringWithUTF8String:chrstr];
[repArray addObject:corpRepresentative];
}
chrstr =(char *)sqlite3_column_text(selectAllStmt, 1);
if(chrstr !=NULL)
{
corpTitle = [NSString stringWithUTF8String:chrstr];
[titleArray addObject:corpTitle];
}
chrstr =(char *)sqlite3_column_text(selectAllStmt, 2);
if(chrstr !=NULL)
{
corpBusiness_name = [NSString stringWithUTF8String:chrstr];
[businessArray addObject:corpBusiness_name];
}
chrstr =(char *)sqlite3_column_text(selectAllStmt, 3);
if(chrstr !=NULL)
{
corporteUid = [NSString stringWithUTF8String:chrstr];
[uidArray addObject:corporteUid];
}
}
}
sqlite3_finalize(selectAllStmt);
sqlite3_close(db);
}
- (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] autorelease];
UILongPressGestureRecognizer *longPressGesture =
[[[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(longPress:)] autorelease];
[cell addGestureRecognizer:longPressGesture];
}
// Configure the cell...
UILabel *txtLbl = [[UILabel alloc]initWithFrame:CGRectMake(70, 0, 175, 26)];
//[txtLbl setBackgroundColor:[UIColor greenColor]];
[txtLbl setTextAlignment:UITextAlignmentLeft];
NSString *txtStr = [repArray objectAtIndex:indexPath.row];
UIFont *fontTxtLbl = [UIFont fontWithName: #"FuturaMdBT" size:16];
[txtLbl setFont:fontTxtLbl];
[txtLbl setText:txtStr];
[cell.contentView addSubview:txtLbl];
[txtLbl release];
UILabel *txtDetailLbl = [[UILabel alloc]initWithFrame:CGRectMake(70, 27, 175, 20)];
// [txtDetailLbl setBackgroundColor:[UIColor redColor]];
[txtDetailLbl setTextAlignment:UITextAlignmentLeft];
NSString *txtDetailStr = [titleArray objectAtIndex:indexPath.row];
UIFont *fontTxtDetailLbl = [UIFont fontWithName: #"Arial" size:12];
[txtDetailLbl setFont:fontTxtDetailLbl];
[txtDetailLbl setText:txtDetailStr];
[cell.contentView addSubview:txtDetailLbl];
[txtDetailLbl release];
UILabel *txtDetailLbl1 = [[UILabel alloc]initWithFrame:CGRectMake(70, 47, 175, 20)];
//[txtDetailLbl1 setBackgroundColor:[UIColor blueColor]];
[txtDetailLbl1 setTextAlignment:UITextAlignmentLeft];
NSString *txtDetailStr1 = [businessArray objectAtIndex:indexPath.row];
UIFont *fontTxtDetailLbl1 = [UIFont fontWithName: #"Arial" size:12];
[txtDetailLbl1 setFont:fontTxtDetailLbl1];
[txtDetailLbl1 setText:txtDetailStr1];
[cell.contentView addSubview:txtDetailLbl1];
[txtDetailLbl1 release];
// cell.textLabel.text = [repArray objectAtIndex:indexPath.row];
//cell.detailTextLabel.text = [titleArray objectAtIndex:indexPath.row];
cell.imageView.image=[imagearray objectAtIndex:indexPath.row];
UIView *viewSelected = [[[UIView alloc] init] autorelease];
viewSelected.backgroundColor = [UIColor colorWithRed:224.0/255.0
green:229.0/255.0
blue:241.0/255.0
alpha:1.0];
cell.selectedBackgroundView = viewSelected;
cell.textLabel.highlightedTextColor=[UIColor colorWithRed:46.0/255.0
green:77.0/255.0
blue:141.0/255.0
alpha:1.0];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *tempUID = [uidArray objectAtIndex:indexPath.row];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:tempUCID forKey:#"corpUcid"];
[prefs setObject:#"PROFILER" forKey:#"selProfiler"];
CorpPrevFront *corpPrevFront = [[CorpPrevFront alloc]initWithNibName:#"CorpPrevFront" bundle:nil];
[self.navigationController pushViewController:corpPrevFront animated:NO];
[corpPrevFront release];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//FOR VIEW
if (buttonIndex == 1 && [alertView tag] == 1) {
// IT WILL BE MATCH ON CorpPrevFront CLASS
}
//FOR DELETE
if(buttonIndex == 2 && [alertView tag] == 1){
} else if(buttonIndex == 1 && [alertView tag] == 2){
DeleteProfiler *delProfiler = [[DeleteProfiler alloc]init];
BOOL del = [delProfiler deleteProfiler:selectedCpUid];
//[delProfiler deleteProfiler:selectedCpUcid];
if (del == YES) {
[repArray removeObjectAtIndex:selectedIndexPath.section];
[businessArray removeObjectAtIndex:selectedIndexPath.row];
[titleArray removeObjectAtIndex:selectedIndexPath.row];
[ucidArray removeObjectAtIndex:selectedIndexPath.row];
[self.corporateTable reloadData];
}
}
}
I am trying to delete an array element from a method. If I execute the below line then the app crashes automatically in my iPod Touch. But it works fine in simulator without any errors.
[repArray removeObjectAtIndex:selectedIndexPath.row];
or
[repArray removeObjectAtIndex:0];
both crashes the app even if I have more than one elements in array.
When you app crashes like that, 99% of the time you are trying to remove an object beyond the bounds of array.

Making UITableView without IB

I'm making UITableView without IB.
My UITableView has 2 styles UITableViewCell - first row style and other row style.
I use MTLable Class instead of UILabel.
Question:
This code's results is very strange.
First cell style and other cell style is mixed when next page scrolled.
I can't find my code's faults.
I need your advice. Thanks!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//firsT cELL
if(indexPath.row==0)
{
CGRect frame=CGRectMake(0,0,80, 60);
MTLabel *label1=[[MTLabel alloc] init];
label1.backgroundColor = [UIColor grayColor];
label1.frame=frame;
label1.text=#"123";
label1.tag = 1001;
[cell.contentView addSubview:label1];
label1.autoresizingMask = UIViewAutoresizingFlexibleTopMargin &UIViewAutoresizingFlexibleWidth & UIViewAutoresizingFlexibleLeftMargin & UIViewAutoresizingFlexibleRightMargin;
label1.contentMode = UIViewContentModeTopLeft;
[label1 setTextAlignment:MTLabelTextAlignmentRight];
[label1 release];
}
else
{
CGRect frame3=CGRectMake(0,0,80, 60);
MTLabel *label3=[[MTLabel alloc]init];
label3.frame=frame3;
label3.backgroundColor = [UIColor purpleColor];
label3.text=#"100";
label3.tag = 1003;
label3.autoresizingMask = UIViewAutoresizingFlexibleTopMargin &UIViewAutoresizingFlexibleWidth & UIViewAutoresizingFlexibleLeftMargin & UIViewAutoresizingFlexibleRightMargin;
label3.contentMode = UIViewContentModeTopLeft;
[label3 setTextAlignment:MTLabelTextAlignmentRight];
[cell.contentView addSubview:label3];
[label3 release];
}
}
FTS_book *book = [items objectAtIndex:indexPath.row];
if(indexPath.row==0)
{
MTLabel *label1 = (MTLabel*)[cell viewWithTag:1001];
label1.text= [NSString stringWithFormat:#"%d", book.chapter];
}
else
{
MTLabel *label3 = (MTLabel*)[cell viewWithTag:1003];
label3.text= [NSString stringWithFormat:#"%d", book.verse];
}
return cell;
}
Use different cell Id's for those two styles.

Single row selection - UITableView

Good Afternoon,
I'm trying to deploy my application in the selection of a single line in UITableView, but when I select another row, the previous line is not clear. I'm using images to represent the selection of the lines. I've tried to make each user's selection table rows stay with the image of deselection, but got no success.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *EditCellIdentifier = #"EditCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:EditCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:EditCellIdentifier] autorelease];
UILabel *label = [[UILabel alloc] initWithFrame:kLabelRect];
label.tag = kCellLabelTag;
[cell.contentView addSubview:label];
[label release];
UIImageView *imageView = [[UIImageView alloc] initWithImage:unselectedImage];
imageView.frame = CGRectMake(5.0, 10.0, 23.0, 23.0);
[cell.contentView addSubview:imageView];
imageView.hidden = !inPseudoEditMode;
imageView.tag = kCellImageViewTag;
[imageView release];
}
[UIView beginAnimations:#"cell shift" context:nil];
UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:kCellImageViewTag];
NSNumber *selected = [selectedArray objectAtIndex:[indexPath row]];
imageView.image = ([selected boolValue]) ? selectedImage : unselectedImage;
imageView.hidden = !inPseudoEditMode;
[UIView commitAnimations];
UILabel *label = (UILabel *)[cell.contentView viewWithTag:kCellLabelTag];
label.text =[listOfItems objectAtIndex:[indexPath row]];
label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;
label.opaque = NO;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tv deselectRowAtIndexPath:indexPath animated:YES];
if (inPseudoEditMode) {
BOOL selected = [[selectedArray objectAtIndex:[indexPath row]] boolValue];
[selectedArray replaceObjectAtIndex:[indexPath row] withObject:[NSNumber numberWithBool:!selected]];
[tv reloadData];
}
}
I guess you want to allow only one row selected at a time.
Here is what I suggest in tableView: didSelectRowAtIndexPath::
Mark all row as not selected:
for (int i = 0; i < [selectedArray count]; i++) {
[selectedArray replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:NO]];
}
Then, mark the current row as selected:
[selectedArray replaceObjectAtIndex:[indexPath row] withObject:[NSNumber numberWithBool:YES]];
OR, do it all in the same for loop:
for (int i = 0; i < [selectedArray count]; i++) {
NSNumber rowSelected = [NSNumber numberWithBool:(indexPath.row == i)];
[selectedArray replaceObjectAtIndex:i withObject:rowSelected];
}
Hope this is what you want!
Shortest way which i use
for (int i = 0; i < [selectedArray count]; i++) {
[selectedArray replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:i==indexpath.row?YES:NO]];
}

Can't Get Data Loaded on First Row of UITableView

I'm trying to parse an HTML Data using HTMLParser (by Ben Reeves) and display results on UITableView. For some reasons I'm able to show up results only on last row of the tableView. Here's the code snippet:
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSData *responseData = [request responseData];
NSError *error = [request error];
HTMLParser *parser = [[HTMLParser alloc] initWithData:responseData error:&error];
HTMLNode *bodyNode = [parser body];
arrayNodes = [bodyNode findChildrenWithAttribute:#"class" matchingName:#"foo" allowPartial:NO];
for (HTMLNode *arrayNode in arrayNodes) {
NSString *footitle = [arrayNode allContents];
NSLog(#"%#", footitle);
fooLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 200, 30)];
fooLabel.text = (#"%#", footitle);
fooLabel.textColor = [UIColor blackColor];
}
[self.fooTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arrayNodes count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
// Configure the cell.
// [self.arrayNodes objectAtIndex:indexPath.row];
[cell.contentView addSubview:fooLabel];
return cell;
}
Where am I making the mistake?
for (HTMLNode *arrayNode in arrayNodes) {
NSString *footitle = [arrayNode allContents];
NSLog(#"%#", footitle);
fooLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 200, 30)];
fooLabel.text = (#"%#", footitle);
fooLabel.textColor = [UIColor blackColor];
}
You are creating fooLabel's with same Frame size and location as many as arrayNodes array.
then in [cell.contentView addSubview:fooLabel]; it is showing you the last value that label is updated with.
take out that fooLabel from for loop.
in your cellForRowAtIndexPath:
HTMLNode* arrayNode = [arrayNodes objectAtIndex:[indexPath row]];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[arrayNode allContents]];
You need to create the foolabel in your cellForRowAtindexPath method. Right now only one foolabel is created, and it is applied to each cell in turn and then set to the next cell, leaving it placed on the last cell. So instead you should do this:
-
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
// Configure the cell.
HTMLNode *arrayNode = [arrayNodes objectAtIndex:indexPath.row];
NSString *footitle = [arrayNode allContents];
UILabel *fooLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 200, 30)];
fooLabel.text = (#"%#", footitle);
fooLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview:fooLabel];
[fooLabel release];
return cell;
}