Custom UITableViewCell in static UITableView - objective-c

I've defined a custom UITableViewCell that I'm using in my static UITableView. I'd like to access the variables I've defined (myTableViewImageCell, etc) but doing it from tableView:willDisplayCell seems to say something along the lines of "redefinition of cell type.
Here's what I'm doing:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.myTableViewLabel setShadowColor:[UIColor whiteColor]];
[cell.myTableViewLabel setShadowOffset:CGSizeMake(0.0f, 1.0f)];
}

If all of your cells are the same custom class, you can just change the definition of your method to indicate that and get rid of your first line (which is wrong anyway):
- (void)tableView:(UITableView *)tableView willDisplayCell:(ANMyTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.myTableViewLabel setShadowColor:[UIColor whiteColor]];
[cell.myTableViewLabel setShadowOffset:CGSizeMake(0.0f, 1.0f)];
[cell.myTableViewLabel setTextColor:[UIColor hex666Color]];
}
Note however, that if you are doing the same thing to all of your cells (as you show here), you should do this in cellForRowAtIndexPath: instead of willDisplayCell:. That way, the code is run only once when the cell is created instead of every time that it is displayed.

FYI, the problem is the extraneous ;cell; you have at the end of the second line. change it to
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
ANMyTableViewCell *cell = (ANMyTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.myTableViewLabel setShadowColor:[UIColor whiteColor]];
[cell.myTableViewLabel setShadowOffset:CGSizeMake(0.0f, 1.0f)];
[cell.myTableViewLabel setTextColor:[UIColor hex666Color]];
}

Related

Tableview won't reload data after selecting row

When I select any row in tableView data should be removed. I have set selected text in textField which is working, but tableView data is not cleared after selection, code given below.
Please help me.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tblViewSearch deselectRowAtIndexPath:indexPath animated:YES];
TimeZoneCity *objCity = (TimeZoneCity *)[searchResult objectAtIndex:indexPath.row];
txtfieldTimeZone.text=objCity.timezone;
[searchResult removeAllObjects];
[self.tblViewSearch reloadData];
}
If you really want to remove the table view you could try this. This is a bad idea if you have set up your table view with interface builder, since it does the creation for you in this case.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tblViewSearch deselectRowAtIndexPath:indexPath animated:YES];
TimeZoneCity *objCity = (TimeZoneCity *)[searchResult objectAtIndex:indexPath.row];
txtfieldTimeZone.text=objCity.timezone;
[self.tblViewSearch removeFromSuperview];
}
A better strategy might be to just hide it, and then show it again when needed. To make it look nicer you could add an animation too.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tblViewSearch deselectRowAtIndexPath:indexPath animated:YES];
TimeZoneCity *objCity = (TimeZoneCity *)[searchResult objectAtIndex:indexPath.row];
txtfieldTimeZone.text=objCity.timezone;
[UIView animateWithDuration:0.3 animations:^{
self.tblViewSearch.alpha = 0.0;
}];
}

How to add a content view on UITAbleViewCell when a row is selected?

I'm trying to add a button when a row is selected in UITableView and also remove it when row is tapped second time, i don't want custom UITableViewCell.
Any suggestion or sample code will be appreciated.
code i've tried:
in cellForRowAtIndexPath method
if(cell.selected == YES){
UITextField* numOfBottles =[[UITextField alloc] initWithFrame:CGRectMake(240,9.0f,50, 25)];
numOfBottles.tag = indexPath.row;
[numOfBottles setBorderStyle:UITextBorderStyleNone];
[numOfBottles setBackgroundColor:[UIColor clearColor]];
[numOfBottles setTextColor:[UIColor whiteColor]];
[numOfBottles setTextAlignment:UITextAlignmentLeft];
[numOfBottles setBackground:[UIImage imageNamed:#"blue_dropdown_normal.png"]];
[numOfBottles setFont:[UIFont systemFontOfSize:16.0f]];
[numOfBottles setDelegate:self];
NSString* quantity = [[NSString alloc] initWithString:[subtotalObj.qtyArray objectAtIndex:(indexPath.row - 1)]];
[numOfBottles setText:quantity];
[numOfBottles setTextAlignment:UITextAlignmentCenter];
[numOfBottles setBackgroundColor:[UIColor whiteColor]];
numOfBottles.keyboardType = UIKeyboardTypeDefault;
// numOfBottles.tag = indexPath.row;
[cell.contentView addSubview:numOfBottles];
[numOfBottles release];
}
and in didSelectRowAtIndexPath method
[tableView reloadData];
But still button(textfield with background image) is not rendered.
I think the best way is to use a UITableViewCell subclass.
But you can use my answer on another question to make your TableView rerender and place a button in your cell.
- (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];
}
if(indexPath.row == selectedRow){ // if your criteria where met
//add your button
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//do your select things here
selectedRow = indexPath.row; //where selectedRow is a class variable
[self.tableView reloadData]; // rerenders the tableview
}
There's UITableView delegate, - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath implement it, where you'll get the row which user tapped, get the current cellusing cellForRowAtIndexPath: you need to pass NSIndexPath using the section and row. Now check if cell.contentView subviews has UIButton which you want? If yes, perform removeFromSuperView else addSubView. If you cell have already filled with UIButtons you can give unique tag to distinguish them.
Use these delegates
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
//Create reference of button with tag 999
cell.contentView addSubView:yourButtonReference]
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
UIButton *btnAdded = (UIButton *)[cell viewWithTag:999];
if(btnAdded){
[btnAdded removeFromSuperView];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
}

UITableViewCell configuration

I have a UITableView in my app, which stores a list of URLs. Above table there are UITextField and UIButton.
When user types some URL on textField and then presses the button I add those URL like the top element in the tableView.
Next, when user selects any of URLs, I want to create a button within selected UITableViewCell to let user to follow that URL (actually, it's not important). Here's didSelectRowAtIndexPath method definition:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.recentUrlsTableView cellForRowAtIndexPath:indexPath];
UIButton *button = [UIButton new];
button.frame = CGRectMake(cell.frame.size.width - 34, cell.frame.origin.y + 10, 24, 24);
button.backgroundColor = [UIColor lightGrayColor];
[button setTitle:#">" forState:UIControlStateNormal];
[cell.contentView addSubview:button];
[button release];
}
But I want those button to disappear when I deselect cell. So, I tried this thing (I kow it looks stupid, but unfortunately I didn't know better way to find my button in there):
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.recentUrlsTableView cellForRowAtIndexPath:indexPath];
for (UIView *subview in cell.subviews)
{
if ([subview isKindOfClass:[UIButton class]])
if ([[(UIButton*)subview titleLabel].text isEqualToString:#">"])
{
[subview removeFromSuperview];
[subview release];
}
}
}
But this stuff seemed to work incorrectly - instead of releasing and disappearing at all, button started to in another rows - even those, which were empty.
After that I tried to do same actions for every row in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method. But it also didn't help.
I guess, that there are some trivial issue, but I can't find it. So, I will highly appreciate any of your help!
When you scroll up/down the table cells get reused and cell (with button) can be populated with completely diffetent contents (i.e. is used for different indexpath). That's why you're experiencing this.
So there is always only one button (or none) on your table.
You'd be best off if you create (reatin & keep reference of it) one button from outside the table. Your didDeselectRowAtIndexPath would then look like:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.recentUrlsTableView cellForRowAtIndexPath:indexPath];
//you have outside reference for this button
[button removeFromSuperView];
button.frame = CGRectMake(cell.frame.size.width - 34, cell.frame.origin.y + 10, 24, 24);
[cell.contentView addSubview:button];
}
If this doesn't work you should use combination of reloadData and cellForIndexPath. Like so:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.recentUrlsTableView reloadData];
}
- (void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//create your cell as usually
if ([cell isSelected]) { //i'm not sure if this is the right way to detect selected cell
[button removeFromSuperView];
button.frame = CGRectMake(cell.frame.size.width - 34, cell.frame.origin.y + 10, 24, 24);
[cell.contentView addSubview:button];
}
}

UIView background color does not take effect

Can someone tell me why this is not working right?
I have these lines of code below within my table view cell for tableView:didSelectAtIndexRowPath: method.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[tableView viewWithTag:199]removeFromSuperview];
CGSize cellSize = [tableView cellForRowAtIndexPath:indexPath].frame.size;
UIView *subSelectionView = [[UIView alloc]initWithFrame:CGRectMake(10, 0, (int)cellSize.width - 20, 100)];
[subSelectionView setBackgroundColor:[UIColor blueColor]];
subSelectionView.layer.borderColor = [UIColor grayColor].CGColor;
subSelectionView.layer.borderWidth = 1;
subSelectionView.tag = 199;
[[tableView cellForRowAtIndexPath:indexPath]addSubview:subSelectionView];
}
Notice the code:
[subSelectionView setBackgroundColor:[UIColor blueColor]];
Obviously, I want to change the color of the subview that I added to the UITableViewCell but why is it not working ?
jus reload the table after adding the subview on the table
[[tableView cellForRowAtIndexPath:indexPath]addSubview:subSelectionView];
[tableview reloadData];
Add the following line to the start of the method
[tableView deselectRowAtIndexPath:indexPath animated:NO];
So ur method would be
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[[tableView viewWithTag:199]removeFromSuperview];
....
}

UITableView and Selecting Items

I am currently creating a simple grouped UITableView that when the user selects for example "Apple" I would like a image I have in my project to be loaded into a Image View of a Apple.
I have seen some examples on the internet but I am trying to see what else is out there.
Any suggestions would be greatly appreciated. Thanks.
Either set the cell.imageView.highlightedImage, or in the didSelectRowAtIndexPath method, change the cell.imageView.image from nil to some [UIImage imageNamed:#"myImage.png"]:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCustomCellID = #"com.me.foo.cell";
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
cell.textLabel.highlightedTextColor = [UIColor blueColor];
cell.imageView.image = nil;
cell.imageView.highlightedImage = [UIImage imageNamed:#"myImage.png"];
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(iPadRootViewControllerTableCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Do this if you didnt set the highlightedImage in cellForRowAtIndexPath
// [self tableView:tableView cellForRowAtIndexPath:indexPath].imageView.image = [UIImage imageNamed:#"myApple.png"];
}