UITableView: Removing cells - objective-c

Guys, i have a little issue here. Im doing a search to my UITableView. I search "iPhone", it returns to me one result. Its ok. iPhone just has one result. But when i search for iPad, that has two results, it gives me 2 results, but the first row stills displaying iphone info. The second display the iPad correct information.
When i click on search bar again to search another word, it gives me the two desired rows of ipad. But when i press the cancel button, it returns to the iPhone and iPad rows.
What im doing wrong?
Thanks
Cell and Table Methods:
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.listCod count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"SearchResult";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell =
[[[UITableViewCell alloc]
initWithFrame:CGRectMake(0, 0, 180, 200)
reuseIdentifier:MyIdentifier]
autorelease];
UIColor *bkgColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"SearchViewTableCellBackground.png"]];
UIView* backgroundView = [ [ [ UIView alloc ] initWithFrame:CGRectZero ] autorelease ];
backgroundView.backgroundColor = bkgColor;
cell.backgroundView = backgroundView;
// Foto do produto
UIView *teste = [[UIView alloc] init];
teste.frame = CGRectMake(5, 5, 100, 100);
teste.backgroundColor = [UIColor blueColor];
[cell addSubview:teste];
// Label de Marca
UILabel *marca = [[UILabel alloc] init];
marca.frame = CGRectMake(115, 10, 195, 25);
marca.backgroundColor = [UIColor clearColor];
marca.textColor = [UIColor grayColor];
NSString *marcaString = [self.listMarca objectAtIndex:indexPath.row];
marca.font = [UIFont systemFontOfSize:13.0];
marca.text = marcaString;
[cell addSubview:marca];
[marca release];
// Label do nome do produto
UILabel *nome = [[UILabel alloc] init];
nome.frame = CGRectMake(115, 30, 195, 25);
nome.backgroundColor = [UIColor clearColor];
nome.textColor = [[UIColor alloc] initWithRed:26.0 / 255 green:177.0 / 255 blue:240.0 / 255 alpha:1.0];
nome.font = [UIFont boldSystemFontOfSize:25.0];
NSString *noemString = [self.listName objectAtIndex:indexPath.row];
nome.text = noemString;
[cell addSubview:nome];
[nome release];
//Preco
UILabel *preco = [[UILabel alloc] init];
preco.frame = CGRectMake(115, 55, 195, 25);
preco.backgroundColor = [UIColor clearColor];
preco.textColor = [UIColor grayColor];
// Manda preco e parcela pra tratar
[self priceFormat:[self.listPreco objectAtIndex:indexPath.row] :[self.listParcela objectAtIndex:indexPath.row]];
preco.font = [UIFont boldSystemFontOfSize:20.0];
preco.text = self.precoFinal;
[cell addSubview:preco];
//Parcela
UILabel *parcelaLabel = [[UILabel alloc] init];
parcelaLabel.frame = CGRectMake(115, 73, 195, 25);
parcelaLabel.backgroundColor = [UIColor clearColor];
parcelaLabel.textColor = [UIColor grayColor];
parcelaLabel.font = [UIFont systemFontOfSize:13.0];
parcelaLabel.text = self.parcelamentoFinal;
[cell addSubview:parcelaLabel];
}
return cell;
}
And the search bar methods:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES animated:YES];
self.theTableView.allowsSelection = NO;
self.theTableView.scrollEnabled = NO;
[theTableView setRowHeight:110];
}
- (void)searchDisplayController:(UISearchDisplayController *)controller
willShowSearchResultsTableView:(UITableView *)tableView
{
[tableView setRowHeight:[[self theTableView] rowHeight]];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.tableData removeAllObjects];
[self.theTableView reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searchBar.text=#"";
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
self.theTableView.allowsSelection = YES;
self.theTableView.scrollEnabled = YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
return NO;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
// Converte a String do campo de busca
NSString* buscaGet =
[searchBar.text stringByAddingPercentEscapesUsingEncoding:
NSASCIIStringEncoding];
// Conecta com a URL
NSString *endereco = [[NSString alloc] initWithFormat:#"http://localhost/icomm/test.php?nome=%#", buscaGet];
// Lê o resultado
TBXML * tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:endereco]] retain];
TBXMLElement * rootXMLElement = tbxml.rootXMLElement;
TBXMLElement * item = [TBXML childElementNamed:#"produto" parentElement:rootXMLElement];
listCod = [[NSMutableArray alloc] init];
listMarca = [[NSMutableArray alloc] init];
listName = [[NSMutableArray alloc] init];
listPreco = [[NSMutableArray alloc] init];
listParcela = [[NSMutableArray alloc] init];
while (item) {
// Seta a id do produto
TBXMLElement * codigo = [TBXML childElementNamed:#"item" parentElement:item];
NSString * codProd = [TBXML textForElement:codigo];
[self.listCod addObject:codProd];
// Seta a marca do produto
TBXMLElement * marca = [TBXML childElementNamed:#"marca" parentElement:item];
NSString * marcaProd = [TBXML textForElement:marca];
[self.listMarca addObject:marcaProd];
// Seta o nome do produto
TBXMLElement * nome = [TBXML childElementNamed:#"nome" parentElement:item];
NSString * nomeProd = [TBXML textForElement:nome];
[self.listName addObject:nomeProd];
// Seta o preco do produto
TBXMLElement * preco = [TBXML childElementNamed:#"preco" parentElement:item];
NSString * precoProd = [TBXML textForElement:preco];
[self.listPreco addObject:precoProd];
// Seta o parcela do produto
TBXMLElement * parc = [TBXML childElementNamed:#"parcela" parentElement:item];
NSString * parcProd = [TBXML textForElement:parc];
[self.listParcela addObject:parcProd];
// Procura o proximo produto.
item = [TBXML nextSiblingNamed:#"produto" searchFromElement:item
];
}
NSLog(#"%#", self.listName);
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
self.theTableView.allowsSelection = YES;
self.theTableView.scrollEnabled = YES;
//Remove tudo da table e recarrega
[[[self searchDisplayController] searchResultsTableView] performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO];
[tableData removeAllObjects];
[theTableView reloadData];
}
Thanks for all.

You are only setting your labels up inside the if (cell == nil) part of your code. This only gets called the first time a cell is created; after that the cell is dequeued for reuse and you don't create a new cell. If you want to dequeue your cells like that, you need to set the text (and any other property of the label that might change) outside of that if statement. In order to get references to your labels, you will need to use tags and viewWithTag:.
Or if you want to simplify things, don't dequeue your tableCells; though this could lead to performance issues if you have a lot of cells.

Related

NSTableView add Button or Any Other custom object

I Am trying to add Button programatically to NSTableView which is also defined programatically, no luck so far. My latest Code is:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
auto dataRow = _tasks[row];
if([tableColumn.identifier isEqualToString:#"Id"]) {
NSTextField* text = [[NSTextField alloc] init];
text.stringValue = [NSString stringWithUTF8String:dataRow[0].c_str()];
return text;
} else if([tableColumn.identifier isEqualToString:#"Name"]) {
NSTextField* text = [[NSTextField alloc] init];
text.stringValue = [NSString stringWithUTF8String:dataRow[1].c_str()];
return text;
} else if([tableColumn.identifier isEqualToString:#"Check"]) {
NSTableCellView* cellView = [[NSTableCellView alloc] initWithFrame:NSMakeRect(0, 0, 200, 100)];
NSPopUpButtonCell* popupButtonCell = [[NSPopUpButtonCell alloc] init];
[popupButtonCell setPullsDown:YES];
[popupButtonCell insertItemWithTitle:#"dsadsa1" atIndex:0];
[popupButtonCell insertItemWithTitle:#"dsadsa2" atIndex:1];
NSPopUpButton* popupButton = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(0, 0, 200, 100) pullsDown:YES];
popupButton.cell = popupButtonCell;
[popupButton insertItemWithTitle:#"dsadsa" atIndex:0];
[popupButton insertItemWithTitle:#"dsadsa" atIndex:1];
NSTextField* text = [[NSTextField alloc] init];
NSTextFieldCell* textCell = [[NSTextFieldCell alloc] initTextCell:#"dsadsad"];
text.stringValue = [NSString stringWithUTF8String:"titleee"];
text.cell = textCell;
[cellView addSubview:text];
[cellView addSubview:popupButton];
return cellView;
}
the last column is showing empty for some reason

UicollectionView didSelectItemAtIndexPath is not called in uiTableviewcell

I am working on chat view. For this I have used this code :Chat Code
This is working fine. Now I have used UIcollectionView in UItableViewCell. Collection view is working fine. But the issue is didselect method is not called of UICollectionView as well as UITableView. Please help me. I need your help very badly.
I have used this code in UITableViewCell Class:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor];
if ([[UIDevice currentDevice].systemVersion floatValue] < 7.0f) {
self.textLabel.backgroundColor = [UIColor whiteColor];
}
self.textLabel.font = [UIFont systemFontOfSize:14.0f];
self.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.textLabel.numberOfLines = 0;
self.textLabel.textAlignment = NSTextAlignmentLeft;
self.textLabel.textColor = [UIColor blackColor];
_timestampLabel = [[UILabel alloc] init];
_timestampLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_timestampLabel.textAlignment = NSTextAlignmentCenter;
_timestampLabel.backgroundColor = [UIColor clearColor];
_timestampLabel.font = [UIFont systemFontOfSize:12.0f];
_timestampLabel.textColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0];
_timestampLabel.frame = CGRectMake(0.0f, 12, self.bounds.size.width, 18);
[self.contentView addSubview:_timestampLabel];
messageBackgroundView = [[UIImageView alloc] initWithFrame:self.textLabel.frame];
[self.contentView insertSubview:messageBackgroundView belowSubview:self.textLabel];
self.AvatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5,10+TOP_MARGIN, 50, 50)];
[self.contentView addSubview:self.AvatarImageView];
CALayer * l = [self.AvatarImageView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:self.AvatarImageView.frame.size.width/2.0];
self.selectionStyle = UITableViewCellSelectionStyleNone;
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
[self.collectionView registerNib:[UINib nibWithNibName:#"ImageCollection" bundle:nil] forCellWithReuseIdentifier:#"Cell"];
[self.collectionView setUserInteractionEnabled:YES];
self.collectionView.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:self.collectionView];
[messageBackgroundView setUserInteractionEnabled:YES];
[self.contentView setUserInteractionEnabled:YES];
// UITapGestureRecognizer *lpgr // = [[UITapGestureRecognizer alloc] // initWithTarget:self action:#selector(tapRecognized:)]; // lpgr.numberOfTapsRequired
= 1; // lpgr.delegate = self; // [self.collectionView addGestureRecognizer:lpgr];
}
[self setUserInteractionEnabled:YES];
return self; }
In UIViewController I have used this code in tableView cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *L_CellIdentifier = #"SPHTextBubbleCell";
self.documentsArray = [[NSMutableArray alloc]init];
self.documentsArray = [[self.messagesArray objectAtIndex:indexPath.row]valueForKey:#"Attachments"];
SPHTextBubbleCell *cell = [tableView dequeueReusableCellWithIdentifier:L_CellIdentifier];
cell.userInteractionEnabled = YES;
if (cell == nil)
{
cell = [[SPHTextBubbleCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:L_CellIdentifier];
}
// cell.bubbletype=(([[[self.messagesArray objectAtIndex:indexPath.row]valueForKey:#"MessageFromId"]intValue] == 1))?#"LEFT":#"RIGHT";
if ([[[self.messagesArray objectAtIndex:indexPath.row]valueForKey:#"MessageFromId"]intValue] == 1) {
cell.bubbletype = #"RIGHT";
}
else
{
cell.bubbletype = #"LEFT";
}
[cell setBackgroundColor:[UIColor clearColor]];
cell.textLabel.text = [[self.messagesArray objectAtIndex:indexPath.row]valueForKey:#"Content"];
cell.textLabel.tag=indexPath.row;
NSString *dateString = [NSString stringWithFormat:#"%#",[self mfDateFromDotNetJSON:[[self.messagesArray objectAtIndex:indexPath.row]valueForKey:#"UpdatedOn"]]];
NSLog(#"dateString..%#",dateString);
NSString *myString = [NSString stringWithFormat:#"%#",dateString];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss ZZZ";
NSDate *yourDate = [dateFormatter dateFromString:myString];
// NSTimeZone *utc = [NSTimeZone timeZoneWithAbbreviation:#"UTC"];
// [dateFormatter setTimeZone:utc];
dateFormatter.dateFormat = #"dd-MMM-yy HH:mm";
NSString *newString = [dateFormatter stringFromDate:yourDate];
NSLog(#"newString..%#",newString);
if ([self.documentsArray count]!=0)
{
cell.CustomDelegate = self;
[cell.collectionView setUserInteractionEnabled:YES];
[cell.collectionView setDelegate:self];
[cell.collectionView setDataSource:self];
cell.collectionView.delegate =self;
cell.collectionView.dataSource = self;
[cell.collectionView reloadData];
[cell.collectionView setHidden:NO];
[cell.collectionView setBackgroundColor:[UIColor clearColor]];
}
else
{
[cell.collectionView setHidden:YES];
}
cell.collectionView.tag =indexPath.row;
cell.timestampLabel.text = newString;
[cell.AvatarImageView sd_setImageWithURL:([[[self.messagesArray objectAtIndex:indexPath.row]valueForKey:#"MessageFromId"]intValue] == 1)?[NSURL URLWithString:[NSString stringWithFormat:#"%#%#",imageURLLive,[[[self.messagesArray objectAtIndex:indexPath.row]valueForKey:#"SellerPicture"] valueForKey:#"PictureUrl"]]]:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#",imageURLLive,[[[self.messagesArray objectAtIndex:indexPath.row]valueForKey:#"BuyerPicture"] valueForKey:#"PictureUrl"]]]
placeholderImage:[UIImage imageNamed:#"Nav-profile1.png"]];
return cell;
}
Thanks in advance.
If your problem is that your UICollectionView taps do not cause the UITableViewCell it is in to be selected then you can bypass this by subclassing UICollectionView and modifying the hitTest function to your liking.
See my answer here.
This one lets you tap on anything outside of collection view items to select the table cell, but collection view items themselves will block the taps and process them as needed.
Modifying the hitTest method wasn't working in my case. I decided to use UITapGestureRecognizer.
// Custom UITableViewCell
override func awakeFromNib() {
super.awakeFromNib()
let tapGR = UITapGestureRecognizer(target: self, action: #selector(collectionViewTapped(_:)))
tapGR.numberOfTapsRequired = 1
self.collectionView.addGestureRecognizer(tapGR)
}
func collectionViewTapped(gr: UITapGestureRecognizer) {
let point = gr.locationInView(self.collectionView)
if let indexPath = self.collectionView.indexPathForItemAtPoint(point) {
// Do stuff
}
}
Don't forget to set delegate and dataSource.
self.tableView.delegate = self;
self.tableView.dataSource = self;

UITableView section not reloading when called

I want a section of my table view to reload whenever the ViewWillAppear method is called, I've implemented this like so:
- (void)viewWillAppear:(BOOL)animated {
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:0 inSection:1];
reloadRows = [NSArray arrayWithObjects:rowToReload, nil];
[self.tableView reloadRowsAtIndexPaths:reloadRows withRowAnimation:UITableViewRowAnimationNone];
}
Here is the rowforsection method that indicates which content should appear in each tableview section:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"fadk");
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"PINGAS"];
[self.tableView setAlwaysBounceVertical:YES];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:#"PINGAS"] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
// if ([indexPath section] == 0) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 3, 300, 41)];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 0, 300, 120)];
UIView *paddingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 0)] autorelease];
paddingView.backgroundColor = [UIColor cyanColor];
// if ([indexPath row] == 0) {
if ([indexPath section] == 0) {
NSLog(#"0");
[cell addSubview:textField];
if ([indexPath row] == 0) {
textField.placeholder = #"Title";
}
else{
textField.placeholder = #"Location";
}
}
else if ([indexPath section] == 1) {
NSLog(#"1");
NSDateFormatter *formatter;
NSString *eSString1;
NSString *eEString2;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"h:mm a"];
cell.textLabel.text = #"Starts\nEnds";
cell.textLabel.numberOfLines = 2;
eSString1 = [formatter stringFromDate:eSTime];
eEString2 = [formatter stringFromDate:eEtime];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#\n%#", eSString1, eEString2];
cell.detailTextLabel.numberOfLines = 2;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else{
NSLog(#"2");
[cell addSubview:textView];
}
textField.delegate = self;
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;
textField.adjustsFontSizeToFitWidth = YES;
textField.textColor = [UIColor blackColor];
textField.keyboardType = UIKeyboardTypeAlphabet;
textField.returnKeyType = UIReturnKeyDone;
textField.backgroundColor = [UIColor clearColor];
textField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
textField.textAlignment = UITextAlignmentLeft;
textField.tag = 0;
//playerTextField.delegate = self;
textField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
[textField setEnabled: YES];
[textField release];
textView.delegate = self;
textView.textColor = [UIColor blackColor];
textView.keyboardType = UIKeyboardTypeAlphabet;
textView.returnKeyType = UIReturnKeyDone;
textView.backgroundColor = [UIColor clearColor];
textView.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textView.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
textView.textAlignment = UITextAlignmentLeft;
textView.tag = 0;
[textView release];
// }
}
return cell;
}
This works swimmingly the first load, and I after the first calling of viewWillAppear, but after that the section seems to recycle the data from the first load and the second load, and while it still enters the cellforrow section, it no longer goes into the section I call in the viewWIllAppear section.
The reload should be sandwiched between begin / end updates:
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:reloadRows withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];

populate data in iphone dynamically

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [self.LoadFile count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 4;
}
int title =0;
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
for (title = 0 ; title <= section; title++)
{
NSLog(#"section - %d ",title);
NSDictionary *dict = [LoadFile objectAtIndex:title];
NSString *titlename = [[NSString alloc]initWithFormat:#"%#",[dict valueForKey:#"name"]];
NSLog(#"%#",titlename);
if (section == title)
return titlename;
}
}
Here i am using two arrays which is created on the page itself,in this case it will work fine but for the case when i need it to populate from DB then what should i do.
- (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] autorelease];
while (i < sec)
{
dict = [LoadFile objectAtIndex:0];
NSString *addr = [[NSString alloc]initWithFormat:#"%#",[dict valueForKey:#"address"]];
NSString *con = [[NSString alloc]initWithFormat:#"%#",[dict valueForKey:#"contact"]];
NSString *sal = [[NSString alloc]initWithFormat:#"%#",[dict valueForKey:#"salary"]];
NSString *tec = [[NSString alloc]initWithFormat:#"%#",[dict valueForKey:#"tech"]];
NSArray *arr = [[NSArray alloc] initWithObjects:addr,con,sal,tec,nil];
// NSArray *arr = [[NSArray alloc] initWithObjects:i,i,i,i,nil];
//NSArray *arr2 = [[NSArray alloc] initWithObjects:#"12",#"22",#"32",#"42",nil];
NSLog(#"%#",arr);
if(indexPath.section == i)
{
cell.textLabel.text = [arr objectAtIndex:indexPath.row];
i++;
}
}
return cell;
}
for (i = 0 ; i <= sec-1; i++)
{
NSLog(#"section - %d ",title);
NSDictionary *dict = [LoadFile objectAtIndex:title];
if (indexPath.section == 0)
{
UILabel *Address = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
Address.textColor = [UIColor blueColor];
Address.backgroundColor = [UIColor clearColor];
[Address setFont:[UIFont boldSystemFontOfSize:14.0]];
[Address setTag:i];
Address.text = [dict objectForKey:#"address"];
[cell.contentView addSubview:Address];
UILabel *tech = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
tech.textColor = [UIColor blueColor];
tech.backgroundColor = [UIColor clearColor];
[tech setFont:[UIFont boldSystemFontOfSize:14.0]];
[tech setTag:i];
tech.text = [dict objectForKey:#"tech"];
[cell.contentView addSubview:tech];
}
else if (indexPath.section == 1)
{
UILabel *tech = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
tech.textColor = [UIColor blueColor];
tech.backgroundColor = [UIColor clearColor];
[tech setFont:[UIFont boldSystemFontOfSize:14.0]];
[tech setTag:i];
tech.text = [dict objectForKey:#"tech"];
[cell.contentView addSubview:tech];
}
}
else if (indexPath.section == 0)
{
UILabel *contact = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
contact.textColor = [UIColor blueColor];
contact.backgroundColor = [UIColor clearColor];
[contact setFont:[UIFont boldSystemFontOfSize:14.0]];
contact.text = [dict objectForKey:#"contact"];
// NSLog(#"Val-%#",[dict objectForKey:#"name"]);
[contact setTag:-4];
[cell.contentView addSubview:contact];
}
else if (indexPath.section == 0)
{
UILabel *salary = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
salary.textColor = [UIColor blueColor];
salary.backgroundColor = [UIColor clearColor];
[salary setFont:[UIFont boldSystemFontOfSize:14.0]];
salary.text = [dict objectForKey:#"salary"];
// NSLog(#"Val-%#",[dict objectForKey:#"name"]);
[salary setTag:-4];
[cell.contentView addSubview:salary];
}
else
{
UILabel *tech = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
tech.textColor = [UIColor blueColor];
tech.backgroundColor = [UIColor clearColor];
[tech setFont:[UIFont boldSystemFontOfSize:14.0]];
tech.text = [dict objectForKey:#"tech"];
// NSLog(#"Val-%#",[dict objectForKey:#"name"]);
[tech setTag:-4];
[cell.contentView addSubview:tech];
}
I want to populate data from table in database ,dynamically
Once your array has been updated with new data, you just call [self.tableview reloadData];

how can i make a uitableview container to bounce within the view

ok, so this is pretty simple one, but i hope i could explain this clearly - i have a table view that i would like to inset into a container, and then have the table bounces when it reaches the top / bottom. So far, I was able to put my table in a container, but the container is fixed on the view, while the table inside the container bounces. Again, I am looking for a way to fix the table to the container, while having the container bouncing.
Here is what I was able to do, following the code:
What I want to accomplish is to have the black box bouncing rather than the table within it.
my ViewDidLoad in the view controller .m:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//General View Setup
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"backgroundimage.png"]];
self.view.backgroundColor = background;
//Table View Data
listOfItems = [[NSMutableArray alloc] init];
NSArray *appleComputers = [NSArray arrayWithObjects:#"iPhone",#"iPod",#"MacBook",#"MacBook Pro",nil];
NSDictionary *appleComputersDict = [NSDictionary dictionaryWithObject:appleComputers forKey:#"Computers"];
NSArray *otherComputers = [NSArray arrayWithObjects:#"HP", #"Dell", #"Windows", #"Sony", #"Ivory", #"IBM", nil];
NSDictionary *otherComputersDict = [NSDictionary dictionaryWithObject:otherComputers forKey:#"Computers"];
[listOfItems addObject:appleComputersDict];
[listOfItems addObject:otherComputersDict];
self.navigationItem.title = #"Computers";
// Create a table
tblSimpleTable.delegate = self;
CGRect cgRct = CGRectMake(10, 50, 300, 300);
tblSimpleTable = [[UITableView alloc] initWithFrame:cgRct style:UITableViewStyleGrouped]; // Initilize the table
[tblSimpleTable setBackgroundColor:[UIColor blackColor]];
tblSimpleTable.sectionHeaderHeight = 30.0;
tblSimpleTable.sectionFooterHeight = 30.0;
tblSimpleTable.delegate = self;
tblSimpleTable.dataSource = self;
[self.view addSubview:tblSimpleTable];
//Create the header
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)];
headerLabel.text = NSLocalizedString(#"Header for the table", #"");
headerLabel.textColor = [UIColor whiteColor];
headerLabel.shadowColor = [UIColor yellowColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:22];
headerLabel.backgroundColor = [UIColor clearColor];
[containerView addSubview:headerLabel];
self.tblSimpleTable.tableHeaderView = containerView;
}
why don’t you use UIScrollView for that.
I had tested your code & done required changes. Hope you like it.
Code :
(this is your .h file)
#import <UIKit/UIKit.h>
#interface tableScrollViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource, UIScrollViewDelegate> {
UITableView *tblSimpleTable;
NSMutableArray *listOfItems;
NSMutableArray *appleComputers,*otherComputers;
UIScrollView *scrollView;
}
#end
(this is your .m file)
- (void)viewDidLoad {
[super viewDidLoad];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 600)];
scrollView.delegate = self;
scrollView.backgroundColor = [UIColor grayColor];
scrollView.contentSize = CGSizeMake(300, 800);
appleComputers = [[NSMutableArray alloc] init]; // I made it by my style
[appleComputers addObject: #"iPhone"];
[appleComputers addObject:#"iPod"];
[appleComputers addObject:#"MacBook"];
[appleComputers addObject:#"MacBook Pro"];
otherComputers = [[NSMutableArray alloc] init];
[otherComputers addObject: #"HP"];
[otherComputers addObject:#"Dell"];
[otherComputers addObject:#"Windows"];
[otherComputers addObject:#"Sony"];
[otherComputers addObject:#"Ivory"];
[otherComputers addObject:#"IBM"];
self.navigationItem.title = #"Computers";
// Create a table
tblSimpleTable.delegate = self;
CGRect cgRct = CGRectMake(10, 50, 300, 600);
tblSimpleTable = [[UITableView alloc] initWithFrame:cgRct
style:UITableViewStyleGrouped]; // Initilize the table
[tblSimpleTable setBackgroundColor:[UIColor blackColor]];
tblSimpleTable.sectionHeaderHeight = 30.0;
tblSimpleTable.sectionFooterHeight = 30.0;
tblSimpleTable.scrollEnabled = NO;
tblSimpleTable.delegate = self;
tblSimpleTable.dataSource = self;
[scrollView addSubview:tblSimpleTable];
self.view = scrollView;
//Create the header
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)];
headerLabel.text = NSLocalizedString(#"Header for the table", #"");
headerLabel.textColor = [UIColor whiteColor];
headerLabel.shadowColor = [UIColor yellowColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:22];
headerLabel.backgroundColor = [UIColor clearColor];
[containerView addSubview:headerLabel];
tblSimpleTable.tableHeaderView = containerView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
return [appleComputers count];
else if(section == 1)
return [otherComputers count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.section == 0)
cell.text = [NSString stringWithFormat:#"%#“,
[appleComputers objectAtIndex:indexPath.row]];
else if(indexPath.section == 1)
cell.text = [NSString stringWithFormat:#"%#“,
[otherComputers objectAtIndex:indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath
{
// do whatever here
}