problem with check mark in iphone
I am new to the iPhone. I'm creating an application. For that I want to send friends an invitation from my app to a Facebook profile. My Facebook friends are listed in a Tab开发者_JAVA百科leView.
I want to select 40 fiends at a time and send invitation to friends' wall.
In my app I wrote to display my friends:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Displayfriendscell";
Displayfriendscell *cell = (Displayfriendscell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[Displayfriendscell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Configure the cell...
NSString *name=[Friendsarray objectAtIndex:indexPath.row];
//NSLog(@"name is%@",friendid);
cell.nameLabel.text = name;
return cell;
}
What should I do for that? I have no idea about check marks and their workings. I just want an idea about it. Can anybody help me implement this?
Basically, cells have an accessoryType for checkmark you can use: Unselected cells start off as no accessory type:
cell.accessoryType = UITableViewCellAccessoryNone;
When a cell is selected:
cell.accessoryType = UITableViewCellAccessoryCheckmark;
Also, somewhere you'll need to keep track of which items in your datasource (I.e. Friendsarray) have been selected, since the UITableView will reuse it's cells. For example another array filled with NSNumbers - 0 for unselected and 1 for selected (another example would be to change Friendsarray to a 2 dimensional array. Or, since your integrating with facebook, it probably would be best in the long run to create a class of 'friend' and make an array of that, where one of the fields is 'selected').
Since UITableView reuses cells as you scroll the table, your cellForRowAtIndexPath will need to check with your array to see if the cell should be set to checked or unchecked.
The code for selecting the cell goes in:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
When the user completes you'll loop through your datasource (your array or whatever) and get the selected friends.
Hope that's the guidance you're looking for.
What you want to implement is multiple row selection. Look at these links
Is it possible to configure a UITableView to allow multiple-selection?
Select multiple rows in UITableview
This might help you:
in your cellForRowAtIndexPath method do like this:
NSString *name=[Friendsarray objectAtIndex:indexPath.row];
UILabel *lblName = [[UILabel alloc]initWithFrame:CGRectMake(10, 8, 250, 20)];
lblName.text = name;
[cell addSubview:lblName];
UIButton *btnCheck = [UIButton buttonWithType:UIButtonTypeCustom];
btnCheck.frame = CGRectMake(290, 8, 16, 16);
[btnCheck setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
[btnCheck setTitle:@"0" forState:UIControlStateNormal];
[btnCheck addTarget:self action:@selector(btnClicked:)forControlEvents:UIControlEventTouchUpInside];
btnCheck.tag = indexPath.row+1;
[cell addSubview:btnCheck];
// in selector method (btnClicked):
-(void)btnClicked:(id)sender
{
UIButton *btnTemp = (UIButton*)sender;
UIButton *btnSelected = (UIButton*) [self.view viewWithTag:btnTemp.tag];
NSString *strTitle = btnSelected.titleLabel.text;
if([strTitle isEqualToString:@"0"])
{
[btnSelected setTitle:@"1" forState:UIControlStateNormal];
[btnSelected setImage:[UIImage imageNamed:@"checkbox-checked.png"] forState:UIControlStateNormal];
}
else
{
[btnSelected setTitle:@"0" forState:UIControlStateNormal];
[btnSelected setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
}
}
// use two image, one is small sqaure and another with tickmarked image
精彩评论