UITableView multiple selection - iphone

I have a UITableView that I want to use to allow multiple selections. I have rolled together most of the functionality, but am having one small problem.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Selected");
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
[selectedCell setBackgroundColor:[UIColor grayColor]];
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[[selectedCell textLabel] setTextColor:[UIColor whiteColor]];
} else {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
[selectedCell setBackgroundColor:[UIColor clearColor]];
[[selectedCell textLabel] setTextColor:[UIColor blackColor]];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
This is the code I am using to keep the cell selected, however when the user taps on the cell, the aaccessory view turns white and then turns back to blue.
What is the best way for me to keep the accessory view white?
Thanks

I believe easy way to achieve this is by creating custom imageview with checkmark image(you need to get it as white checkmark) and set it as accessory view when selected.
This will avoid the standard behavior of the accessoryview and achieve your goal.

Related

How to change the position of a UISwitch in a TableView

I have a TableView that has UISwitches in it. The switches are slightly off the screen. how do I make the display correctly.
Here is my code that displays the switches.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"POICell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(!cell){
//set the cell text to the
cell.textLabel.text = [self.catNames objectAtIndex:[indexPath row]];
NSString *toggle = [self.toggleArray objectAtIndex:[indexPath row]];
//add switch
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchView;
if ([toggle isEqualToString: #"OFF"]) {
[switchView setOn:NO animated:NO];
}else{
[switchView setOn:YES animated:NO];
}
[switchView addTarget:self action:#selector(switchChanged: ) forControlEvents:UIControlEventValueChanged];
// Configure the cell...
}
return cell;
}
Rather than adding a UISwitch to the standard cell, I'd create a custom UITableViewCell that contained the text label and the switch.
But after looking at the Apple docs, it seems what you want to achieve is a valid approach.
Table View Programming Guide for iOS
And your code already looks a lot like this SO question: How to create a UITableViewCell with a UISwitch and get the data? So I am not sure why the UISwitch isn't displaying as expected.
I think the position of the default accessoryView can't be moved without a problem, as the TableViewCell will reposizion it in many situations.
So adding it as a subview might be the better idea, instead of trying to "hack" the default behavior.

UITableView reload data issues

I have had couple of encounters of this and what I am trying to do is basically calling tableView reloadData however not all the values in the cell is getting updated.The top 5 rows is always not updated... I'd have to scroll to the bottom and then up again to get it updated. Why is this happening?
Here's my code:
- (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];
[cell.textLabel setFont:[UIFont fontWithName:#"Arial" size:16]];
}
//add a button to set to accessory view
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
//I missed setting the frame yesterday!
//[button setFrame:CGRectMake(200, 20, 20, 20)];
[button setImage:[UIImage imageNamed:#"addsource.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(changeButton:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundColor:[UIColor clearColor]];
cell.accessoryView = button;
if ([[self.content objectAtIndex:indexPath.row] isKindOfClass:[Source class]]){
Source * source = [self.content objectAtIndex:indexPath.row];
if (![source.type isEqualToString:#"featured"]){
[cell.textLabel setText:source.domain];
NSURL* URL = [NSURL URLWithString:source.imageUrl];
[cell.imageView setImageWithURL:URL
placeholderImage:[UIImage imageNamed:#"placeholder.jpg"]];
}
}
return cell;
}
and this is called everytime I refresh the whole data set:
[self.content removeAllObjects];
[self.content addObjectsFromArray:objects];
[self.tableView reloadData];
try debugging the following line:
if (![source.type isEqualToString:#"featured"]){
obviously your cell won't get updated in the case where your source type is "featured". double check all values in your content array
I've not dug into your code, but this sounds like a symptom you might see when cells are being reused without being fully reset from their previous appearance.
When a cell gets dequeued, it doesn't get reset, it just gets removed from the table view. So unless you do reset it when it next happens to be allocated, it carry previous config and settings.
So, when you configure a cell, make sure you restore all aspects of the cell in all cases, not just those settings for a particular variant of the cell you happen to be returning this time.
Hope that helps.

adjusting UITableView's cell at runtime

I have subclassed a UITableViewCell. What I want to have is when I click on the cell, I added a UIView to the bottom of the cell and adjust the height of the cell accordingly. Here's my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCell* cell = (MyCell *) [self.table cellForRowAtIndexPath:indexPath];
UIView * options = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height - 27, cell.frame.size.width, 27)];
UIButton* button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[options addSubview:button1];
UIButton* button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[options addSubview:button2];
UIButton* button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[options addSubview:button3];
//did some layout calculation here to position the button
[cell addSubview:options];
}
Here's a video that illustrates the issue
Also I've tried to change the accessoryView of my cell from my custom UIButton to the regular UITableViewCellAccessoryDisclosure but it didn't work
MyCell * cell = (MyCell *)[self.table cellForRowAtIndexPath:temp];
[cell.accessoryView removeFromSuperview];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
If you're curious what I am trying to do, I am trying to create a tweetbot uitableviewcell, where you press the cell/row and it presents you with other options (retweets, etc) and also it was shown animated.
I don't know if I correctly understand what you are trying to do. But if you do want to animate the change of the UITableViewCell's height try to adjust it within - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath and call
[tableView beginUpdates];
[tableView endUpdates];
Put controls into another cell and use
insertRowsAtIndexPaths:withRowAnimation:
to show it and
deleteRowsAtIndexPaths:withRowAnimation:
to hide it.

How can I highlight a UITableViewCell

I'd like very much to temporarily highlight a UITableViewCell to draw attention to the fact that the containing data has changed.
there is a UITableViewCell method: -setHighlighted:(BOOL) animated:(BOOL) but I can't make it do anything?
Here is the relevant part of the view controller:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
switch (indexPath.section) {
case 0: {
if (indexPath.row == 0) {
cell = [[UITableViewCell alloc ] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"currentLoc"];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = #"This is the special cell";
}
[cell setHighlighted:YES animated:YES];
[cell setHighlighted:NO animated:YES];
//[cell setNeedsDisplay];
} else {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"setLocAutomatically"];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = #"Foo";
}
} break;
case 1: {
cell = [[UITableViewCell alloc ] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"selectCity"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if (indexPath.row == 0) {
cell.textLabel.text = #"Select a Bar";
} else {
cell.textLabel.text = #"Select a Baz";
}
} break;
}
[cell autorelease];
return cell;
}
See the [cell setHighlight...] above for my attempt.
Much to my frustration, the cell doesn't highlight and I haven't figured out any way to make it work.
Thank you,
Carl C-M
just put the highlighting / background changing / whatever code in:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
Because you're doing all this in tableView:cellForRowAtIndexPath:, nothing happens to the cell on-screen until after you return the cell. Therefore, setting the cell to be highlighted does nothing visible to the user.
What you want to do is figure out a way to set the cell as highlighted after you return it from the method. Perhaps looking into NSObject's performSelector:withObject:afterDelay: method might do you some good - you could set the cell as highlighted with a delay, so it gets returned, displayed, then highlighted.
Have you thought of altering the UILabel for the cell and simply changing the appearance rather than trying to alter the cell itself. Possible a different text color or bold?
That would allow you to keep the changes within tableView:cellForRowAtIndexPath:
This also ensures the the default appearance of highlighting isn't mixed with your additional message of changed information. Highlighting can mean a lot of things, but text formatting can be used to indicate your specific concept.
UILabel * textLabel = [yourCell textLabel];
// From here you can alter the text
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UILabel_Class/Reference/UILabel.html
try using the
[tblView selectRowAtIndexPath:myIndex animated:YES scrollPosition:UITableViewScrollPositionTop];
function.
I've got a variation on this that is continuing to stump me. I have a wizard that uses tables to indicate possible selections. Once a selection is made, I'd like to temporarily highlight the selected row, then advance to the next frame in the wizard.
I do the advancement in
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
But I can't make the highlight happen.
Any suggestions?
Try this:
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (//tell if cell is marked) {
[cell setBackgroundColor:[UIColor colorWithRed:158.0/255.0 green:28.0/255.0 blue:36.0/255.0 alpha:1.0]];
[[cell textLabel] setTextColor:[UIColor whiteColor]];
if ([cell detailTextLabel]) {
[[cell detailTextLabel] setTextColor:[UIColor whiteColor]];
}
}
else
{
[cell setBackgroundColor:[UIColor whiteColor]];
[[cell textLabel] setTextColor:[UIColor blackColor]];
if ([cell detailTextLabel]) {
[[cell detailTextLabel] setTextColor:[UIColor blackColor]];
}
}
}
use this in the uitableviewdelegate and then when you want to highlight the cell mark the cell then call reloadData on the tableview

Refresh NSTableView After Click - Not Refreshing

I have an app with a UITableView, using both icons and disclosure buttons. I want to update the icon on a row with a "selected" icon, and update the previously-selected row with an "unselected" icon. I have the code in place, but when I click on the rows, it sets both rows to the "selected" state, even though via debugging I can see that my state variable is being set to the correct row. If I keep clicking rows I can sometimes get the "unselected" state to show. I suspect it's a refresh issue, but I've tried the setNeedsDisplay method on the cells and the tableView itself, but with no luck. Anyone run into this before? BTW, this is in the simulator (2.2.1) - haven't tried it on the device.
Here's the code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int newRow = [indexPath row];
int oldRow = [lastIndexPath row];
if (newRow != oldRow)
{
[[tableView cellForRowAtIndexPath:indexPath] setImage: [UIImage imageNamed:#"IsSelected.png"]];
c_oListPtr.c_sCurItem = [[tableView cellForRowAtIndexPath:indexPath] text];
[[tableView cellForRowAtIndexPath:lastIndexPath] setImage: [UIImage imageNamed:#"NotSelected.png"]];
[lastIndexPath release];
lastIndexPath = indexPath;
[[tableView cellForRowAtIndexPath:lastIndexPath] setNeedsDisplay];
[[tableView cellForRowAtIndexPath:indexPath] setNeedsDisplay];
[tableView setNeedsDisplay];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Thanks
-Mike
Have you tried [tableView reloadData]?