I assign some custom view to UITableViewCell.accessoryView, but if I scroll the tableView crazy, some accessoryView will disappear in iOS 7, and if I touch the cell, it's accessoryView can appear, I don't know why, because it's correct in iOS 6. This is my code, someone can help me?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"CELL %d", (int)indexPath.row+1];
NSDictionary * dict = [_dataSource objectAtIndex:indexPath.row];
if ([dict objectForKey:#"switch"])
{
cell.accessoryView = [dict objectForKey:#"switch"];
}
else
{
cell.accessoryView = nil;
}
return cell;
}
When we use ReusableCellWithIdentifier in table view it reuse cells in table, you set cell.accessoryView = nil; it's remove accessory view for all cells in table view with same CellIdentifier try this code, it's solve your problem :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
NSDictionary * dict = [_dataSource objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryView = [dict objectForKey:#"switch"];
}
cell.textLabel.text = [NSString stringWithFormat:#"CELL %d", (int)indexPath.row+1];
if ([dict objectForKey:#"switch"])
{
cell.accessoryView.hidden=NO;
}
else
{
cell.accessoryView.hidden=YES;
}
return cell;
}
Related
I have the following implementation, where I am adding a footerview as a customcell. However, if there is no content, I do not want to show the last cell, if there is I want to show it.
In my current implementation, it always displays the last cell.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.adminOrderElements count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return adminOrderElements[section].products.count + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row < adminOrderElements[indexPath.section].products.count)
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSArray * tempArray = adminOrderElements[indexPath.section].products;
cell.textLabel.text = [[tempArray objectAtIndex:indexPath.row] objectForKey:#"productname"];
return cell;
}
else
{
static NSString *CellIdentifier = #"FooterCell";
FooterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[FooterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if(adminOrderElements[indexPath.section].expanded && [adminOrderElements[indexPath.section].notes length]>0)
{
cell.footerLabel.text = [NSString stringWithFormat:#"Notes: %#", adminOrderElements[indexPath.section].notes];
}
else
{
cell.heightConstraints.constant = 1;
cell.footerLabel.text = #"";
}
return cell;
}
}
There is one option that you can make cellHeight to 0.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(CHECK_IF_NO_CONTAIN)
return 0;
return 40;// Normal height as you want
}
I think you can add condition in else clause like if certain condition is met then you want to display this cell other wise not...
if (indexPath.row < adminOrderElements[indexPath.section].products.count)
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSArray * tempArray = adminOrderElements[indexPath.section].products;
cell.textLabel.text = [[tempArray objectAtIndex:indexPath.row] objectForKey:#"productname"];
return cell;
}
else //Here add your condition here if you have content for the section like I think this condition might work [adminOrderElements[indexPath.section].notes length]>0
{
static NSString *CellIdentifier = #"FooterCell";
FooterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[FooterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if(adminOrderElements[indexPath.section].expanded && [adminOrderElements[indexPath.section].notes length]>0)
{
cell.footerLabel.text = [NSString stringWithFormat:#"Notes: %#", adminOrderElements[indexPath.section].notes];
}
else
{
cell.heightConstraints.constant = 1;
cell.footerLabel.text = #"";
}
return cell;
}
}
Its quite simple with UITableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (dataObjects.count == 0){
return 0;
}
return dataObjects.count;
}
I am new to swift.
How can I set IndentationLevel in UITableViewCell?
I have already achieved through Objective C, but I am not able to achieve through Swift.
EDIT:
- (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.textLabel.text=[[self.menuTableAr objectAtIndex:indexPath.row] valueForKey:#"name"];
[cell setIndentationLevel:[[[self.menuTableAr objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
return cell;
}
Thanks
Assume that valueForKey will return a NSNumber Object
Try
let level = self.menuTableAr.objectAtIndex(indexPath.row).valueForKey("level") as! NSNumber
cell.indentationLevel = Int(level.intValue)
I am using two UITableView in same View. I have used prototype cell in which there are two label to show date and venue in TableView.
Here is the code what I did. Both TableViews showing same data.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableView *tableViewOne=(UITableView*)[self.view viewWithTag:5];
UITableView *tableViewtwo=(UITableView*)[self.view viewWithTag:6];
UITableViewCell *cell;
if (tableViewOne) {
NSLog(#"In Table1");
static NSString *CellIdentifier = #"cell";
cell=[tableViewOne dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel * dateLabel = (UILabel*)[cell viewWithTag:111];
dateLabel.text = [dateArrayLableInTableviewOne objectAtIndex:indexPath.row];
UILabel *venueLabel=(UILabel*)[cell viewWithTag:114];
venueLabel.text=[venueArrayLabelInTableViewOne objectAtIndex:indexPath.row];
}
else if(tableViewtwo){
NSLog(#"In Tabl2 ");
static NSString *CellIdentifier2 = #"cellTwo";
cell = [tableViewtwo dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
UILabel * dateLabelOfTableViewTwo = (UILabel*)[cell viewWithTag:211];
dateLabelOfTableViewTwo.text = [dateArrayLabelInTableViewTwo objectAtIndex:indexPath.row];
UILabel *venueLabelOfTableViewTwo=(UILabel*)[cell viewWithTag:214];
venueLabelOfTableViewTwo.text=[venueArrayLabelInTableViewTwo objectAtIndex:indexPath.row];
}
return cell;
}
Your if condition is incorrect. So please check like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (tableView.tag == 5) // Change here
{
NSLog(#"In Table1");
static NSString *CellIdentifier = #"cell";
cell=[tableViewOne dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel * dateLabel = (UILabel*)[cell viewWithTag:111];
dateLabel.text = [dateArrayLableInTableviewOne objectAtIndex:indexPath.row];
UILabel *venueLabel=(UILabel*)[cell viewWithTag:114];
venueLabel.text=[venueArrayLabelInTableViewOne objectAtIndex:indexPath.row];
}
else
{
NSLog(#"In Tabl2 ");
static NSString *CellIdentifier2 = #"cellTwo";
cell = [tableViewtwo dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
UILabel * dateLabelOfTableViewTwo = (UILabel*)[cell viewWithTag:211];
dateLabelOfTableViewTwo.text = [dateArrayLabelInTableViewTwo objectAtIndex:indexPath.row];
UILabel *venueLabelOfTableViewTwo=(UILabel*)[cell viewWithTag:214];
venueLabelOfTableViewTwo.text=[venueArrayLabelInTableViewTwo objectAtIndex:indexPath.row];
}
return cell;
}
Do it like
if (tableView == tableViewOne){
// Your Code
}
elseif (tableView == tableViewTwo){
// Your Code
}
Your comaprison is incorrect since both table view are present, first condition get satisfied always thus same data in both tables.
Compare using UITableView object identifiers
if (tableView==first_table_view_name) {
}
else
{
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//UITableView *tableViewOne=(UITableView*)[self.view viewWithTag:5];
//UITableView *tableViewtwo=(UITableView*)[self.view viewWithTag:6];
UITableViewCell *cell;
if (tableView.tag==5) // problem is here
{
NSLog(#"In Table1");
static NSString *CellIdentifier = #"cell";
cell=[tableViewOne dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel * dateLabel = (UILabel*)[cell viewWithTag:111];
dateLabel.text = [dateArrayLableInTableviewOne objectAtIndex:indexPath.row];
UILabel *venueLabel=(UILabel*)[cell viewWithTag:114];
venueLabel.text=[venueArrayLabelInTableViewOne objectAtIndex:indexPath.row];
}
else if(tableView.tag==6){
NSLog(#"In Tabl2 ");
static NSString *CellIdentifier2 = #"cellTwo";
cell = [tableViewtwo dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
UILabel * dateLabelOfTableViewTwo = (UILabel*)[cell viewWithTag:211];
dateLabelOfTableViewTwo.text = [dateArrayLabelInTableViewTwo objectAtIndex:indexPath.row];
UILabel *venueLabelOfTableViewTwo=(UILabel*)[cell viewWithTag:214];
venueLabelOfTableViewTwo.text=[venueArrayLabelInTableViewTwo objectAtIndex:indexPath.row];
}
return cell;
}
I am new to UITableViews, and I dont know very much about this topic.
I have two buttons in the same View, but not in the Table, for control the switch between the UITableViewCells, when one button is pressed then set a boolean for load the specific Cell.
If you click the button 1 then it shows the table with the CellType1.
If you click the button 2 then it shows the table with the CellType2.
My goal is use the same Table for this. But I dont know where to start.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
if (clickButton1 == true)
{
clickButton1 *cell = (clickButton1*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CellType1" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.name.text = #"Trial";
return cell;
}
else if (clickButton2 == true)
{
clickButton2 *cell = (clickButton2*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CellType2" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//cell.name.text = #"Trial";
return cell;
} else
I think that this code is not the best. But I am lost at this point.
Thanks in advance!
using a tutorial example like this...
//3</pre>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.tweetsArray count];
}
//4
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//5
static NSString *cellIdentifier = #"SettingsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//6
NSString *tweet = [self.tweetsArray objectAtIndex:indexPath.row];
//7
[cell.textLabel setText:tweet];
[cell.detailTextLabel setText:#"via Codigator"];
return cell;
}
if you have two arrays for items such as self.tweetArray1 and self.tweetArray2 you could change this code to
//3</pre>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int i = buttonNumberClicked;
return [self.tweetsArray[i] count];
}
//4
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
//5
int i = buttonNumberClicked;
static NSString *cellIdentifier = #"SettingsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//6
NSString *tweet = [self.tweetsArray[i] objectAtIndex:indexPath.row];
//7
[cell.textLabel setText:tweet];
[cell.detailTextLabel setText:#"via Codigator"];
return cell;
}
and set up ibactions for your buttons to change the variable/int buttonNumberClicked to 1 or 2 depending on which was pressed and [tableView reloadData]; inside ibaction as well, hope this helps.
I pass an array called userArray which stores another array with strings. This is what I have so far but I know it's wrong. Can someone point me in the right direction?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}
//Set Up Cell
DataSingleton *sharedData = [DataSingleton sharedData];
for (NSArray *array in sharedData.usersArray){
cell.primaryLabel.text = [array objectAtIndex:1];
cell.secondaryLabel.text = [array objectAtIndex:2];
cell.profileImage = [UIImage imageNamed:#"111-user.png"];
return cell;
}
}
cellForRowAtIndexPath is a UITableViewDataSource method, and it asks the data for single cell only. So, you have to remove a loop, and set up a single cell at once, using indexPath.row as an array index in your DataSingleton
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}
//Set Up Cell
DataSingleton *sharedData = [DataSingleton sharedData];
NSArray *array = [sharedData.usersArray objectAtIndex:indexPath.row];
cell.primaryLabel.text = [array objectAtIndex:1];
cell.secondaryLabel.text = [array objectAtIndex:2];
cell.profileImage = [UIImage imageNamed:#"111-user.png"];
return cell;
}
Also, you should implement tableView:numberOfRowsInSection: to return [[[DataSingleton sharedData] usersArray] count]